What does the compile-time error "Undefined symbols for architecture x86_64" mean?

gravity black picture gravity black · Dec 9, 2011 · Viewed 65.2k times · Source

I'm trying to program a graph class using an adjacent list from an example in my C++ text book, and when I compile using this command: Code: g++ -o prog program.cpp ...I get the following error:

Undefined symbols for architecture x86_64:
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

... what in the world does this mean? It may turn out to be an issue with my code, but I feel like it may be deeper than that, because I've gotten this same seemingly inexplicable error for several different projects, many of which were solved in different ways, and unfortunately completely by accident.

I read somewhere that it may have to do with whether I'm using 32 bit or 64 bit libraries, and that the tags -m32 or -m64 may need to be used, but I'm not sure if this applies here. Interestingly enough, when I tried adding the -m64 tag I got the same exact error, but when I tried using the -m32 tag I got the same error, except it said

Undefined symbols for architecture i386:
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

instead.

Mainly I just want to know what in the world the error is saying. I'm used to debugging compile-time errors that give a specific line in the code, etc., but I can't discern anything like that from this. Any ideas?

If it helps, I'm using a late 2008 Macbook with Intel Core 2 Duo, (so 64-bit), and I'm running OS X Lion (10.7.2), which I think is the latest version. Also, I'm using gcc version 4.2.1.

Answer

Nicolas Bachschmidt picture Nicolas Bachschmidt · Jun 27, 2012

When you compile the file, the compiler invokes the linker which tries to generate an executable. But it cannot because you didn't provide a function named main which is the function that will be executed when your program is launched.

Either you don't want to run the linker because you want to compile several files separately then combine then. In that case, use the -c flag to tell the compiler to skip the link stage.

Or either you want to execute the compiled file. Then you need to implement the function main.