Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc 4.8 on MAC OS X 10.8 throws "Undefined symbols for architecture x86_64: "

Tags:

gcc

This is the case even with the old 4.2 GCC (I experienced this when I set up my unofficial iOS toolchain). gcc assumes C by default, and invokes the linker without linking to the C++ standard library; in contrast, g++ assumes C++ and links against the C++ standard library by default.

All in all - possible solutions:

gcc myprog.c -o myprog -lstdc++

or

g++ myprog.c -o myprog

The answer to this stackoverflow question has the answer

gcc and g++ linker

Use

gcc -lstdc++ use_new.cpp -o use_new

The -lstdc++ flag tells the linker to include the C++ Standard Library

http://en.wikipedia.org/wiki/C%2B%2B_Standard_Library

I'm running Mac OS X 10.7.4 and the library is located here

/usr/lib/libstdc++.dylib

This isn't related to the code pasted by @user1582840 just my 2 cents, and from a different cause of the same problem in g++ when working on some of my own code:

I received the "ld: symbol(s) not found for architecture x86_64" error when using g++ 4.2.1 on OS X 10.8/Darwin11 for a different reason. Hopefully this will help some searching google for the problem.

I received this error because I had a Class defined, in the class definition I declared member functions. However, when I defined the member functions I forgot to include the class modifier.

So for an example of what I'm talking about (code sample, not full program):

class NewClass
{
    NewClass(); // default constructor
};

then later, when defining the NewClass() constructor (or any member function) I simply had:

// don't do this, it will throw that error!!
NewClass()
{
    // do whatever
}

rather than:

// proper way
NewClass::NewClass()
{
    // do whatever
}

This is a rather simple mistake, and I managed to catch it in a short amount of time luckily, but it could be easy for someone to miss (us newbies especially), and the solutions about gcc/g++ linkers, XCode, etc. aren't any help for this :P

Again, hope it helps!