某些标准库类与clang ++的链接器错误

Ami*_*mit 6 c++ libstdc++ clang++

我面临一个奇怪的链接器问题clang ++ - 它能够找到std :: string类的定义,而不是std :: ios_base :: failure class的定义.

$ 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
Run Code Online (Sandbox Code Playgroud)

我注意到如果我评论std :: ios_base :: failure的实例化,程序会正确链接(并执行).

$ clang++ foo.cpp && ./a.out
hello world
Run Code Online (Sandbox Code Playgroud)

有人可以帮我理解这种行为以及如何解决这个问题?

PS我也发现了与clang 3.6.0相同的行为.

Rus*_*ene -1

尝试在文件的开头添加 include

#include <ios>
Run Code Online (Sandbox Code Playgroud)

根据cpprefrence它是在标头中定义的<ios>