1

I wanted to try and use clang instead of gcc on my Ubuntu system. I set up the most basic C++ project possible, with a minimal CMakeLists.txt:

cmake_minimum_required(VERSION 3.20)

project(pcb_engine)

add_executable(main main.cpp)

My main.cpp is also very simple:

#include <iostream>

int main() {
    std::cout << "Hello World" << std::endl;
    return 0;
}

No when I run g++ -o main_g++ ../main.cpp everything compiles just fine. Also, if I set my system's default compiler to be gcc and g++ using

sudo update-alternatives --config c++
sudo update-alternatives --config cc

and I run cmake .. and make from my build folder, everything works!


The error:

Now I switch to using clang and clang++ as my default compiler via the update-alternatives command and while the build file generation succeeds, the make command fails with the following error message:

fatal error: 'iostream' file not found
#include <iostream>

When I run clang++ -v -stdlib=libc++ -Wall ../main.cpp -o main_clang manually, I get a linker error:

Ubuntu clang version 14.0.0-1ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Candidate multilib: .;@m64
Selected multilib: .;@m64
 "/usr/lib/llvm-14/bin/clang" -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all --mrelax-relocations -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.cpp -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -mllvm -treat-scalable-fixed-error-as-warning -debugger-tuning=gdb -v -fcoverage-compilation-dir=/home/ivan/Documents/TEST/build -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -internal-isystem /usr/lib/llvm-14/bin/../include/c++/v1 -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wall -fdeprecated-macro -fdebug-compilation-dir=/home/ivan/Documents/TEST/build -ferror-limit 19 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fcolor-diagnostics -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/main-482542.o -x c++ ../main.cpp
clang -cc1 version 14.0.0 based upon LLVM 14.0.0 default target x86_64-pc-linux-gnu
ignoring nonexistent directory "/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/include"
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/llvm-14/bin/../include/c++/v1
 /usr/lib/llvm-14/lib/clang/14.0.0/include
 /usr/local/include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
 "/usr/bin/ld" -pie -z relro --hash-style=gnu --build-id --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o main_clang /lib/x86_64-linux-gnu/Scrt1.o /lib/x86_64-linux-gnu/crti.o /usr/bin/../lib/gcc/x86_64-linux-gnu/12/crtbeginS.o -L/usr/bin/../lib/gcc/x86_64-linux-gnu/12 -L/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../lib64 -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib64 -L/usr/lib/llvm-14/bin/../lib -L/lib -L/usr/lib /tmp/main-482542.o -lc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/bin/../lib/gcc/x86_64-linux-gnu/12/crtendS.o /lib/x86_64-linux-gnu/crtn.o
/usr/bin/ld: /tmp/main-482542.o: undefined reference to symbol '__gxx_personality_v0'
/usr/bin/ld: /lib/x86_64-linux-gnu/libc++abi.so.1: error adding symbols: DSO missing from command line
clang: error: linker command failed with exit code 1 (use -v to see invocation)

The non-verbose error message:

/usr/bin/ld: /tmp/main-f39fec.o: undefined reference to symbol '__gxx_personality_v0'
/usr/bin/ld: /lib/x86_64-linux-gnu/libc++abi.so.1: error adding symbols: DSO missing from command line

Can anyone help me identify where to start fixing this?

Interestingly, when I run clang++ -v -stdlib=libstdc++ -Wall ../main.cpp -o main_clang (note the -stdlib=libstdc++ flag change to the gnu standard library), then I get the same simple fatal error: 'iostream' file not found error as with cmake! So there is a difference in the behavior with the two different standard c++ libraries...


The background:

I come from using gcc, and installed clang via get-apt install clang. I've read that clang does not come with its own standard C++ library, but I have trouble understanding the implications for me: Previously, I had issues with clang finding a correct standard library .so file... I tried some more installs and I have got the libstdc++.so (default gnu) and the libc++.so as well as libc++.a and libc++.abi.so in my /usr/lib/x86_64-linux-gnu folder with a correct symlink hierarchy. For some reason, after a system restart, the /usr/bin/ld: cannot find -lstdc++ error went away, and now I have this linker error where it seems that clang can not find the include header files when running cmake. Now, I am at the end of my limited wisdom.

huzzm
  • 489
  • 9
  • 24
  • Did you try using the -I flag with clang++? Like: clang++ -I /path/to/iostream/header -o main_clang ../main.cpp – kek5chen Dec 15 '22 at 10:05
  • @kek5chen How do I find the path to my std headers? – huzzm Dec 15 '22 at 10:12
  • I ran `clang++ -I /usr/include/c++/11 -stdlib=libc++ -Wall ../main.cpp -o main_clang` and now I get the error: `/usr/include/c++/11/iostream:38:10: fatal error: 'bits/c++config.h' file not found ... #include ` – huzzm Dec 15 '22 at 10:15
  • 2
    `clang++ -v -stdlib=libc++ -Wall ../main.cpp -o main_clang` is a perfectly normal command that should work unless your system is somehow broken. – n. m. could be an AI Dec 15 '22 at 10:16
  • 1
    Does anything change if you set the default compilers back to gcc and g++ and set clang/clang++ via the command line when you configure the project: `CC=clang CXX=clang++ cmake ..` Also, take a look at this: https://stackoverflow.com/questions/54521402/locating-iostream-in-clang-fatal-error-iostream-file-not-found – icebp Dec 15 '22 at 15:44
  • 1
    @icebp Thanks for the link to the other stackoverflow question. The accepted answer there solved the issue for me partially! When I set my default compiler to clang (or set the CC and CXX env variables directly) and use cmake, it now works perfectly fine. CMake seems to autodetect the non-corrupted libstdc++.so (gnu standard library). The same is true for calling clang directly via command line: When I pass the -stdlib=libstdc++ flag, I compiles correctly! Now the only issue left is when I pass -stdlib=libc++... The linker error remains. Bizarre – huzzm Dec 15 '22 at 16:14
  • Does this answer your question? [Locating iostream in Clang++: fatal error: 'iostream' file not found](https://stackoverflow.com/questions/54521402/locating-iostream-in-clang-fatal-error-iostream-file-not-found) – Superlokkus Jul 25 '23 at 06:38

0 Answers0