Linker error with clang++ for some standard library classes
Asked Answered
C

2

6

I am facing a weird linker issue with clang++ - it is able to find the definition of std::string class but not of 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

I noticed that if I comment the instantiation of std::ios_base::failure, the program links (and executes) correctly.

$ clang++ foo.cpp && ./a.out
hello world

Can someone please help me understand this behavior and how to fix it?

P.S. I observed the same behavior with clang version 3.6.0 as well.

Comedic answered 14/7, 2015 at 4:23 Comment(0)
M
0

Assuming you are intent on using clang's own standard-library (libc++), then you are not far off - you will have to provide the include-path of the standard-library (where ever your libcxx-build installed to):

$> clang++ foo.cpp -stdlib=libc++ -I/path/to/libcxx-build/include/c++/v1

(add "-v" switch to the above, if you want verbose output).

P.S. unless the includes go elsewhere for your clang-3.7, the above aught to work - as tested with my clang-3.8

Memorable answered 16/9, 2015 at 9:42 Comment(0)
F
-1

Try adding the include at the beginning of your fie

#include <ios>

According to cpprefrence it is defined in the header <ios>

Fussy answered 14/7, 2015 at 5:22 Comment(1)
No. The question refers to a linker issue, not a compilation issue. Your suggestion is not making any difference.Bagpipes

© 2022 - 2024 — McMap. All rights reserved.