Re: undefined reference to `main'

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Kai Ruottu wrote:
> vinod wrote:
>
>> When I using gcc to compile my code.c to code , I got the error msg as
>> follows :
>> /usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/crt1.o: In function
>> `_start':
>> (.text+0x18): undefined reference to `main'
>> collect2: ld returned 1 exit status
>>
>> What can I do now, any suggestion?
>
> Please read the book :
>
> http://www.crasseux.com/books/ctut.pdf
>
> also a manual for glibc, GCC etc. could be useful in your own library...
> Or reading them online. Like :
>
> http://www.gnu.org/software/libc/manual/
>
The solution for this depends on what you want to do:

1) if you want to build an executable from your source code file code.c,
you must obviously add a main function which then calls on of your
currently used main functions (whatever name that is) - already suggested

2) if main is defined in another source file - which I guess is true in
your case - you need to compile the file code.c only (by using the -c
compiler flag) and then link all object files to your executable.

As an example for the second solution. Assume you have two source files
main.c (defines the main function) and code.c. To build an executable,
you either use

    gcc code.c main.c -o <exec_name>

or, if you want to compile in single steps

    gcc code.c -c (produces code.o)
    gcc main.c -c (produces main.o)
    gcc code.o main.o -c <exec_name>

For the last step, you could theoretically also use ld to link the
executable, but I would not recommend this because you need to specify a
bunch of linker flags which are used by gcc any.

Hope that helps,
Andi


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux