所有,我在我的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;
}
答案 0 :(得分:74)
即使使用旧的4.2 GCC也是如此(我在设置非官方的iOS工具链时经历过这种情况)。 gcc
默认采用C语言,并在不链接到C ++标准库的情况下调用链接器;相反,g++
假设C ++并默认链接C ++标准库。
总而言之 - 可能的解决方案:
gcc myprog.c -o myprog -lstdc++
或
g++ myprog.c -o myprog
答案 1 :(得分:8)
这个stackoverflow问题的答案有答案
使用
gcc -lstdc++ use_new.cpp -o use_new
-lstdc++
标志告诉链接器包含C ++标准库
http://en.wikipedia.org/wiki/C%2B%2B_Standard_Library
我正在运行Mac OS X 10.7.4,库位于此处
/usr/lib/libstdc++.dylib
答案 2 :(得分:3)
这与@ user1582840粘贴的代码无关,只是我的2美分,而且在处理我自己的代码时,g ++中同一问题的原因不同:
由于其他原因,在OS X 10.8 / Darwin11上使用g ++ 4.2.1时,我收到了“找不到架构x86_64的ld:符号”错误。希望这有助于一些搜索谷歌的问题。
我收到此错误是因为我在类定义中定义了一个Class,我声明了成员函数。但是,当我定义成员函数时,我忘了包含类修饰符。
所以我要谈的是一个例子(代码示例,而不是完整的程序):
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
再次,希望它有所帮助!