56

I write code like this in my Mac OS X v10.8 (Mountain Lion), and when I use "gcc use_new.cpp -o use_new" to compile it, it throws a wrong message like this:

Undefined symbols for architecture x86_64:
  "std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))", referenced from:
      _main in ccr2vrRQ.o
  "std::basic_ostream<char, std::char_traits<char> >::operator<<(void const*)", referenced from:
      _main in ccr2vrRQ.o
  "std::basic_ostream<char, std::char_traits<char> >::operator<<(double)", referenced from:
      _main in ccr2vrRQ.o
  "std::basic_ostream<char, std::char_traits<char> >::operator<<(int)", referenced from:
      _main in ccr2vrRQ.o
  "std::basic_ostream<char, std::char_traits<char> >::operator<<(unsigned long)", referenced from:
      _main in ccr2vrRQ.o
  "std::ios_base::Init::Init()", referenced from:
      __static_initialization_and_destruction_0(int, int) in ccr2vrRQ.o
  "std::ios_base::Init::~Init()", referenced from:
      __static_initialization_and_destruction_0(int, int) in ccr2vrRQ.o
  "std::cout", referenced from:
      _main in ccr2vrRQ.o
  "std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)", referenced from:
      _main in ccr2vrRQ.o
  "std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
      _main in ccr2vrRQ.o
  "operator delete(void*)", referenced from:
      _main in ccr2vrRQ.o
  "operator new(unsigned long)", referenced from:
      _main in ccr2vrRQ.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

And when I use "g++ use_new.cpp -o use_new" it is OK. How can I fix this?

#include <iostream>

struct fish
{
    float weight;
    int id;
    int kind;
};

int main()
{
    using namespace std;
    int* pt = new int;
    *pt = 1001;
    cout << "int: " << *pt << "in location: " << pt << endl;
    double* pd = new double;
    *pd = 100000001.0;
    cout << "double: " << *pd << "in location: " << pd << endl;
    cout << "int point pt is length " << sizeof(*pt) << endl;
    cout << "double point pd is length " << sizeof(*pd) << endl;
    delete pt;
    delete pd;
    cout << (int *)"How are you!" << endl;
    return 0;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1582840
  • 561
  • 1
  • 4
  • 4
  • I'm having a similar problem with `i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1`, trying to compile a "Hello world" program. Started bounty. – Randomblue Aug 15 '12 at 19:03
  • 2
    This has been asked in one form or another so many times (for example [here](http://stackoverflow.com/questions/5590983/problem-occurred-while-compiling-a-cpp-program-in-gcc), [here](http://stackoverflow.com/questions/1221902/programs-compiles-in-g-but-exits-with-linker-errors-in-gcc?rq=1) or [here](http://stackoverflow.com/questions/8904722/gcc-linker-cant-find-standard-library)). You would benefit from reading [What is the difference between g++ and gcc?](http://stackoverflow.com/questions/172587/what-is-the-difference-between-g-and-gcc). – Troubadour Aug 18 '12 at 00:43

3 Answers3

115

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
  • I see. Would you recommend using a more recent version of GCC? – Randomblue Aug 15 '12 at 19:29
  • @Randomblue there's no more recent version of GCC - even 4.8 is under development. Either use `-lsdtc++` for linking, or better (I advise you to do that): use g++ for linking. –  Aug 15 '12 at 19:30
  • 3
    I'm using `g++` to run my program and I'm still getting the `undefined symbols for architecture x86_64` – Lily Jan 16 '16 at 03:53
16

The answer to this Stack Overflow 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.

I'm running Mac OS X v10.7.4 (Lion) and the library is located here:

/usr/lib/libstdc++.dylib
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
amdn
  • 11,314
  • 33
  • 45
  • It tells the linker to include the C++ Standard Library, answer edited to include that. – amdn Aug 15 '12 at 19:27
5

This isn't related to the code pasted by @user1582840, just my two 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 v10.8 (Mountain Lion)/Darwin 11 for a different reason.

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3064209
  • 147
  • 4
  • 14