[ale] gcc errors...

Byron A Jeff byron at gemini.cc.gatech.edu
Thu Aug 29 22:50:28 EDT 1996


> 
> This one is for the programming guru's (or such out there)...
> 
> I am taking ye old basic "C" refresher class.  I got permision to
> use linux gcc instead of the quick c on dos.  Problem is I need to
> be able to "compile it" then "link it"...  

Well that's a good thing.

> 
> This is my sample code:
> 
> {1}:oasis:/root/prog/>cat compile.c
> main()
> {
>   int Var;
>   printf ("Hello!\n");
>   printf ("\n\n");
>   printf ("Please enter an integer: ");
>   scanf ("%d",&Var);
>   printf ("\n\n");
>   printf ("You entered %d.\n",Var);
> }

Code looks fine.

> 
> 
> I can do a "gcc compile.c" and it does everything beautifully.

As you should.

> But when I do a "gcc -c compile.c" and "ld compile.c" I get the
> following:
> 
> -rw-r--r--   1 root     root          180 Aug 29 21:54 compile.c
> -rw-r--r--   1 root     root         1092 Aug 29 21:56 compile.o
> {0}:oasis:/root/prog/>ld compile.o
> ld: warning: cannot find entry symbol _start; defaulting to 08048074
> compile.o: In function `main':
> compile.o(.text+0xc): undefined reference to `printf'
> compile.o(.text+0x19): undefined reference to `printf'
> compile.o(.text+0x26): undefined reference to `printf'
> compile.o(.text+0x37): undefined reference to `scanf'
> compile.o(.text+0x44): undefined reference to `printf'
> compile.o(.text+0x55): undefined reference to `printf'
> 
> Anyone got any ideas?

Oooh! Danger Will Robinson! Danger! DANGER!

See the cc command is really a pretty front end to all the tools (compiler,
assembler, linker, some even had a separate optimizer). It sends all
the correct commands and options to the components so you don't have to.

You've gotten bit. A complete compiled C program will have both a startup
routine (missing symbol start) and a stub library to the dynamic C library
(the failed references to printf below).

ld has a buttload of options that gcc feeds it. That's why it works
correctly from cc.  

But in the interest of science let see if we can figure out what gcc feeds
the loader. Fortunately this is easy to do. If you give gcc a '-v' option 
it'll tell you what commands it's issuing.

Alrighty Then! I compiled up your program by replacing the ld command with
a gcc -v command. The results:

flight:~$ gcc -O -c simple.c
flight:~$ gcc -v simple.o
Reading specs from /usr/lib/gcc-lib/i486-linux/2.5.8/specs
gcc version 2.5.8
-----------------------------------------------------------------------------
 ld -dll-verbose -m486 /usr/lib/crt0.o -L/usr/lib/gcc-lib/i486-linux/2.5.8 simpl
e.o -lgcc -lc -lgcc
-----------------------------------------------------------------------------
[ irrelavent stuff deleted... ]
flight:~$                               


Now see the ld command between the dashed lines? That's what ld needs to 
link the program properly. The crt0.o is the runtime/startup routing for
the program. The -L... tells ld where to look for libraries. And the -lgcc ...
are the actual libraries to link with the application.

This is an instance where we're glad that the complexity is hidden a bit.
Each and every C program gets linked in with this stuff.

Hope this gives you a better understanding of the process...

BAJ






More information about the Ale mailing list