[NTLUG:Discuss] Extremely simple C program...

Brian brian-sender-67b5e0 at pongonova.net
Sat Aug 10 23:53:20 CDT 2002


On Sat, Aug 10, 2002 at 11:19:51PM -0500, Wayne Dahl wrote:
> #include <stdio.h>
> 
> void main()
> {
> 	printf("Hello world!");
> }
> 
> So far, so good.  After writing that in gedit and saving it as hello.c,
> I issue gcc -c hello.c and get these messages...
> 
> hello.c: In function `main'
> hello.c:6: warning: return type of `main' is not `int'

That's because main's official prototype is

int main( int argc, char *argv[] )

which is useful for parsing command-line args.  However, it's not mandatory to use
this prototype (as you've discovered), so the compiler is simply warning you about
it.

> Then, when I attempt to link the two by using gcc hello.c hello.o, I get
> these messages...
> 
> 
> hello.c: In function `main'
> hello.c:6: warning: return type of `main' is not `int'
> hello.o: In function `main'
> hello.o(.text+0x0): multiple definition of `main'
> /tmp/ccBEHACq.o(.text+0x0): first defined here
> collect2: ld returned 1 exit status

That's because you've already created an object file (hello.o) from a source file
(hello.c), and you are giving the linker two separate definitions (one from hello.c,
which the compiler dutifully re-compiles, and one from hello.o, which you created in
the previous step).

Simply list your object files as args to gcc, and the linker will link them:

gcc hello.o

Alternatively:

gcc -o hello hello.o

to give your executable a name other than the ever-so-boring a.out.  Or do it all in
one step:

gcc -o hello hello.c

A couple of books I've found very useful during my years of C programming:

"The C Programming Language" (2nd ed.), Kernighan & Ritchie (updated for ANSI C).
"C: A Reference Manual", Harbison & Steele.

Also, the man pages for gcc and ld (the linker) are chock full of good (but somewhat
terse) info about compiler specifics probably not covered in the book you're using.

Have fun!

  --Brian
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 232 bytes
Desc: not available
Url : http://ntlug.org/pipermail/discuss/attachments/20020810/ca4bf81c/attachment.bin


More information about the Discuss mailing list