gcc 4.8 on MAC OS X 10.8 throws “Undefined symbols for architecture x86_64: ”
总之,我在我的Mac OS X 10.8中编写了这样一个代码,当我使用"gcc use ou new.cpp-o use ou new"编译它时,它会抛出这样的错误消息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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++使用u new.cpp-o使用u new"是可以的,谁能帮我!?谢谢!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #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.2 gcc也是如此(我在设置非官方的iOS工具链时就遇到过这种情况)。EDCOX1(1)默认为C,调用链接器而不链接到C++标准库;相反,EDCOX1(2)假定默认为C++,并链接到C++标准库。
综合解决方案:
1 | gcc myprog.c -o myprog -lstdc++ |
或
1 | g++ myprog.c -o myprog |
。
这个stackoverflow问题的答案是
gcc和g++链接器
使用
1 | gcc -lstdc++ use_new.cpp -o use_new |
EDCOX1的0进制标志告诉链接器包含C++标准库。
http://en.wikipedia.org/wiki/c%2b%2bu标准库
我运行的是Mac OS X 10.7.4,图书馆就在这里
1 | /usr/lib/libstdc++.dylib |
号
这与@user1582840粘贴的代码无关,只是我的2美分,而且在处理我自己的代码时,G++中出现相同问题的另一个原因是:
在OS X 10.8/Darwin11上使用G++4.2.1时,由于其他原因,我收到了"ld:symbol not found for architecture x86_64"错误。希望这将有助于一些搜索谷歌的问题。
我收到这个错误是因为我定义了一个类,在类定义中我声明了成员函数。但是,当我定义成员函数时,忘记了包含类修饰符。
因此,举一个我所说的例子(代码示例,而不是完整程序):
1 2 3 4 | class NewClass { NewClass(); // default constructor }; |
稍后,在定义newClass()构造函数(或任何成员函数)时,我只需要:
1 2 3 4 5 | // don't do this, it will throw that error!! NewClass() { // do whatever } |
。
而不是:
1 2 3 4 5 | // proper way NewClass::NewClass() { // do whatever } |
。
这是一个相当简单的错误,幸运的是,我在短时间内成功地抓住了它,但对于某些人来说,很容易错过(尤其是美国新手),关于gcc/g++链接器、xcode等的解决方案对此没有任何帮助:p
再次,希望能有所帮助!