我正在用clang++遇到一个奇怪的链接器问题-它能够找到std::string类的定义,但不能找到std::ios_base::failure类的定义。
$ cat foo.cpp
#include <string>
#include <iostream>
int main()
{
std::string msg = "hello world";
std::ios_base::failure f(msg);
std::cout << msg << std::endl;
return 0;
}
$ clang++ foo.cpp
/tmp/foo-b77625.o: In function `main':
foo.cpp:(.text+0x4d): undefined reference to `std::ios_base::failure::failure(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
clang-3.7: error: linker command failed with exit code 1 (use -v to see invocation)
$ clang++ --version
clang version 3.7.0 (trunk 239466)
Target: x86_64-unknown-linux-gnu
Thread model: posix
我注意到,如果我注释了std::ios_base::failure的实例化,程序将正确链接(并执行)。
$ clang++ foo.cpp && ./a.out
hello world
有人可以帮助我了解此行为以及如何解决此问题吗?
P.S.我在clang 3.6.0版中也观察到了相同的行为。
最佳答案
假设您打算使用clang自己的标准库(libc++),则相距不远-您将必须提供标准库的include-path(安装libcxx-build的位置):
$> clang++ foo.cpp -stdlib=libc++ -I/path/to/libcxx-build/include/c++/v1
(如果要详细输出,请在上面添加“-v”开关)。
P.S.除非包括将您的clang-3.7放在其他地方,否则上述内容不起作用-经我的clang-3.8测试
关于c++ - 某些标准库类的clang++链接器错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31397609/