Score:11

clang++ cannot find iostream

ng flag

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.

hr flag
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?
Thomas avatar
ng flag
@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`)
hr flag
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.
Score:15
ng flag

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.

Andrew avatar
mx flag
O.O Thanks.....
smamran avatar
ph flag
Solved the problem.
Score:0
za flag

On Ubuntu 23.04, clang++ searches for /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++, installing g++-13 fixes the issue.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.