clang iostream - symbol not found

﹥>﹥吖頭↗ 提交于 2019-12-08 15:44:58

问题


So I'm trying to get the clang compiler to work.. my natural first choice of program was the following extremely complex code:

#include <iostream>
using std::cout;    using std::endl;
/* hello world.cpp */
int main()
{
    cout << "Hello, world!" << endl;
    return 0;
}

On the command line I did: clang helloworld.cpp and I get the following nice error:

Undefined symbols for architecture x86_64:
  "std::ios_base::Init::~Init()", referenced from:
      ___cxx_global_var_init in cc-4iziZq.o
  "std::ios_base::Init::Init()", referenced from:
      ___cxx_global_var_init in cc-4iziZq.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 cc-4iziZq.o
  "std::cout", referenced from:
      _main in cc-4iziZq.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 cc-4iziZq.o
  "std::ostream::operator<<(std::ostream& (*)(std::ostream&))", referenced from:
      _main in cc-4iziZq.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

What is wrong? Thanks! -kstruct


回答1:


clang is a C compiler. You need to use clang++ or use the -x c++ flag.




回答2:


clang is a C compiler. You need to use clang++ or use the -x c++ flag.

still need add -lstdc++.

I figured out this by -v option. And I found the diffrence is:

clang++

-lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc

clang

-lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed



来源:https://stackoverflow.com/questions/10156582/clang-iostream-symbol-not-found

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