48

所有,我在我的mac os x 10.8中编写了这样的代码,当我使用“gcc use_new.cpp -o use_new”来编译它时,它会抛出这样的错误消息:

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

当我使用“g++ use_new.cpp -o use_new”时,谁可以帮助我!?谢谢你!

#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;
  }
4

3 回答 3

105

即使是旧的 4.2 GCC 也是如此(我在设置非官方 iOS 工具链时遇到过这种情况)。gcc默认假定为 C,并调用链接器而不链接到 C++ 标准库;相反,g++默认情况下假定 C++ 和 C++ 标准库的链接。

总而言之 - 可能的解决方案:

gcc myprog.c -o myprog -lstdc++

或者

g++ myprog.c -o myprog
于 2012-08-15T19:28:13.850 回答
16

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
于 2012-08-15T19:13:16.560 回答
5

这与@user1582840 粘贴的代码无关,只是我的 2 美分,并且在处理我自己的一些代码时,由于 g++ 中相同问题的不同原因:

由于不同的原因,在 OS X 10.8/Darwin11 上使用 g++ 4.2.1 时,我收到“ld: symbol(s) not found for architecture x86_64”错误。希望这将有助于一些搜索谷歌的问题。

我收到此错误是因为我定义了一个类,在类定义中我声明了成员函数。但是,当我定义成员函数时,我忘记了包含类修饰符。

因此,对于我正在谈论的示例(代码示例,而不是完整程序):

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

然后,在定义 NewClass() 构造函数(或任何成员函数)时,我只需要:

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

而不是:

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

这是一个相当简单的错误,幸运的是,我设法在很短的时间内抓住了它,但有人很容易错过(尤其是我们新手),关于 gcc/g++ 链接器、XCode 等的解决方案是对此没有任何帮助:P

再次,希望它有所帮助!

于 2014-03-04T10:20:28.600 回答