Bug 24241 - undefined reference to main when linking an object file generated from multiple .c files
Summary: undefined reference to main when linking an object file generated from multip...
Status: RESOLVED DUPLICATE of bug 22544
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.0.2
: P2 normal
Target Milestone: 4.0.3
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-10-06 16:45 UTC by Indan
Modified: 2005-10-06 17:27 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Indan 2005-10-06 16:45:12 UTC
When making object files from multiple source files the main function seems to be thrown away or something. When compiling the .c into .o files seperately, and then linking that it works. It used to work with at least gcc 3.4.3.

Minimal testcase:

$ cat main.c 
int main(void)
{
        return 7;
}

$ make
cc -o test.o -c  main.c empty.c
cc -s  test.o   -o test
/usr/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../crt1.o: In function `_start':
: undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [test] Error 1

$ objdump -t test.o 

test.o:     file format elf32-i386

SYMBOL TABLE:
00000000 l    df *ABS*  00000000 empty.c
00000000 l    d  .text  00000000 .text
00000000 l    d  .data  00000000 .data
00000000 l    d  .bss   00000000 .bss
00000000 l    d  .note.GNU-stack        00000000 .note.GNU-stack
00000000 l    d  .comment       00000000 .comment

$ cat empty.c

$ cat Makefile 

default: test

test.o: main.c empty.c
        cc -o test.o -c main.c empty.c

clean:
        rm -f *.o test
Comment 1 Andrew Pinski 2005-10-06 16:46:55 UTC

*** This bug has been marked as a duplicate of 22544 ***
Comment 2 Andrew Pinski 2005-10-06 16:48:07 UTC
Note if you want the compiling multiple .c into one .o files use -combine.
Comment 3 Indan 2005-10-06 17:27:23 UTC
(In reply to comment #2)
> Note if you want the compiling multiple .c into one .o files use -combine.
> 

Thanks for the quick reply and the tip.

Shouldn't gcc either give an error, or restore the old behaviour? Would a patch doing one of the two be accepted? If so, what behaviour is prefered?
Comment 4 Andrew Pinski 2005-10-06 17:44:55 UTC
Subject: Re:  undefined reference to main when linking an object file generated from multiple .c files

> 
> 
> 
> ------- Comment #3 from indan at nul dot nu  2005-10-06 17:27 -------
> (In reply to comment #2)
> > Note if you want the compiling multiple .c into one .o files use -combine.
> > 
> 
> Thanks for the quick reply and the tip.
> 
> Shouldn't gcc either give an error, or restore the old behaviour? Would a patch
> doing one of the two be accepted? If so, what behaviour is prefered?

Error out, like it did in 3.3.x and before, see the duplicated bug for
a little more details.

-- Pinski