Can't find c++ libraries on unix

丶灬走出姿态 提交于 2020-01-24 19:45:07

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!