问题
I've written a simple c++ program, test.cpp:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
cout << s << endl;
return 0;
}
Why does runnning gcc test.cpp -o mytest
give me these errors, and more?
Undefined symbols for architecture x86_64:
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()", referenced from:
_main in cc8rGYVq.o
"std::cin", referenced from:
_main in cc8rGYVq.o
回答1:
Don't use the executable named gcc
to compile and link C++ programs; you must use g++
. Not only does it select the appropriate compiler options, it also links with the right libraries for your language (which is the problem you're having here.)
回答2:
"gcc" command compiles C code, in order to compile C++ code you should use "g++"
来源:https://stackoverflow.com/questions/10867164/cant-find-c-libraries-on-unix