我正在尝试编译一些非常简单的C++代码,每次尝试都会出现此错误:
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 ccdb9vrE.o
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()", referenced from:
_main in ccdb9vrE.o
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()", referenced from:
_main in ccdb9vrE.o
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int)in ccdb9vrE.o
"std::ios_base::Init::~Init()", referenced from:
___tcf_0 in ccdb9vrE.o
"std::cin", referenced from:
_main in ccdb9vrE.o
"std::cout", referenced from:
_main in ccdb9vrE.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 ccdb9vrE.o
"std::terminate()", referenced from:
_main in ccdb9vrE.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 ccdb9vrE.o
"std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
_main in ccdb9vrE.o
"std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)", referenced from:
_main in ccdb9vrE.o
"___gxx_personality_v0", referenced from:
Dwarf Exception Unwind Info (__eh_frame) in ccdb9vrE.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
我正在使用以下命令对其进行编译:
$ gcc script.cc
这是脚本的代码:
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Please enter your name:\n";
string name;
cin >> name;
cout << "Hello, " << name << endl;
return 0;
}
而且我确实下载了最新的开发工具。如果有人可以帮助,那就太好了。
最佳答案
使用g++,因为它是c++文件而不是c文件...
它应该以这种方式工作...
关于c++ - 链接C++程序时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15035575/