22

Here is a simple code :

#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
#include <iterator>
#include <iomanip>
#include <cmath>

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

when I want to compile it with g++, there is no issue :

$ g++ test.cpp 
$ ./a.out 
Hello, world!

but if I want to do it with clang++ it doesn't work anymore :

$ clang++ t.cpp               
t.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
         ^~~~~~~~~~
1 error generated.

Here is what returns clang if I add the -v option :

$ clang++ test.cpp -v
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
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Candidate multilib: .;@m64
Selected multilib: .;@m64
Found CUDA installation: /usr/local/cuda, version 
 "/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 test.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/saigre/Bureau -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++ -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/x86_64-linux-gnu -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/backward -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 -fdeprecated-macro -fdebug-compilation-dir=/home/saigre/Bureau -ferror-limit 19 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fcolor-diagnostics -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/test-e9d03e.o -x c++ test.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/../../../../include/c++/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/backward"
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/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++
 /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.
test.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
         ^~~~~~~~~~
1 error generated.

This issue seems to be identical to this post, but no solution from it worked from me. I also found this topic who proposed this solution

clang++ -I/usr/include/c++/11 -I/usr/include/x86_64-linux-gnu/c++/11 -L /usr/lib/gcc/x86_64-linux-gnu/11 test.cpp

working fine. But I have an application that, during its execution compiles a small cpp script using clang and I don't have a hand on the options I can pass to it.

Is there a way to fix the configuration of clang, such that the command clang++ test.cpp works without adding any option ? (it did work in the past)

I'm using ubuntu 22.04, and gcc-12.

3
  • What is your Ubuntu version? Does g++ resolve to g++-12, or to g++-11? Did you install gcc-12 from Ubuntu repositories or from a 3rd party? Commented Jan 11, 2023 at 14:53
  • @steeldriver I'm using ubuntu 22.04, and I think g++ resolves to g++-11 (when I type man gcc, at the end of the manual page, there is written gcc-11) Commented Jan 11, 2023 at 15:19
  • Thanks - you can use g++ --version to confirm. As well, apt policy g++-12 may confirm which repo that comes from. Please use the edit button to add all the information to your question. Commented Jan 11, 2023 at 15:26

2 Answers 2

25

I spoke to a colleague who told me that gcc-12 was included in ubuntu 22.04 an created some mess with clang. To solve the issue, I did

sudo apt install g++-12

and now it works well.

2
  • O.O Thanks..... Commented Feb 22, 2023 at 18:31
  • Solved the problem. Commented Jul 15, 2023 at 4:37
13

As described e.g. here:

Clang and Clang++ "borrow" the header files from GCC & G++. It looks for the directories these usually live in and picks the latest one. If you've installed a later GCC without the corresponding G++, Clang++ gets confused and can't find header files.

It is possible that you have installed some version of the compiler binary, e.g. g++12, without the corresponding C++ standard libraries, e.g. libstdc++-12-dev.

Here is an example from my machine:

Run clang++ -v -E to see which GCCs clang found and which it picked:

Ubuntu clang version 14.0.0-1ubuntu1.1
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
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Candidate multilib: .;@m64
Selected multilib: .;@m64

So it Selected GCC ... version 12.

However, I'm missing the standard libraries for that version:

% ls -1 /usr/include/c++/
11/
9/

I have the standard libraries for 11 and 9, but not 12.

So libstdc++-12-dev fixes i in this case.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.