Locating iostream in Clang++: fatal error: 'iostream' file not found
Asked Answered
B

7

30

I wrote the following simple C++ program:

#include <iostream>

using namespace std;

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

When I compile this with g++, it works perfectly. When I try to compile with Clang++, I get the following error:

main.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
         ^~~~~~~~~~
1 error generated.

Running with the -v parameter, I see the following:

ignoring nonexistent directory "/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/backward"
ignoring nonexistent directory "/include"
ignoring duplicate directory "/usr/include/clang/6.0.0/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++
 /usr/include/clang/6.0.0/include
 /usr/local/include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.

Looking into these folders individually, I found that in /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++ (or, more concisely, in /usr/include/c++) I have the following directories:

drwxr-xr-x   5 root root 4.0K Feb  4 09:38 .
drwxr-xr-x 101 root root  20K Feb  4 12:22 ..
drwxr-xr-x  12 root root  12K May 24  2018 5
drwxr-xr-x  12 root root  12K Oct  9 14:53 7
drwxr-xr-x   5 root root 4.0K Feb  4 09:38 v1
lrwxrwxrwx   1 root root    1 Apr 11  2018 5.5.0 -> 5
lrwxrwxrwx   1 root root    1 Apr 15  2018 7.3.0 -> 7

Within each of the 5, 7, and v1 directories there exists a file called iostream

Also in /usr/include/x86_64-linux-gnu there exists a c++ directory which looks exactly like this one (with 5, 7, 5.5.0, and 7.3.0 directories).

Also in /usr/include there exists a c++ directory which looks exactly like the two above

I'm not sure how my dev environment became such a mess, but at this point I would just like to know how to fix it so that Clang++ will successfully find one of these 9 instances of iostream instead of throwing an error that it doesn't exist. Do I need to add an environment variable to tell Clang where to look? Do I need to pass a command-line parameter to tell Clang to search recursively?

Update (1)

When I try building with libc++ I get the following error:

$> clang++ -stdlib=libc++ main.cpp
/usr/bin/ld: cannot find -lc++abi
clang: error: linker command failed with exit code 1 (use -v to see invocation)

When I try building with the include path manually overridden, I get the following error:

$> clang++ -isystem /usr/include/c++/7 -isystem /usr/include/x86_64-linux-gnu/c++/7 main.cpp
/usr/bin/ld: cannot find -lstdc++
clang: error: linker command failed with exit code 1 (use -v to see invocation)

When I try both, I get the following (incredibly large) error:

$> clang++ -isystem /usr/include/c++/7 -isystem /usr/include/x86_64-linux-gnu/c++/7 -stdlib=libc++ main.cpp
In file included from main.cpp:1:
In file included from /usr/include/c++/7/iostream:39:
In file included from /usr/include/c++/7/ostream:38:
In file included from /usr/include/c++/7/ios:42:
In file included from /usr/include/c++/7/bits/ios_base.h:41:
In file included from /usr/include/c++/7/bits/locale_classes.h:40:
In file included from /usr/include/c++/7/string:52:
In file included from /usr/include/c++/7/bits/basic_string.h:6352:
In file included from /usr/include/c++/7/ext/string_conversions.h:41:
In file included from /usr/include/c++/7/cstdlib:77:
/usr/include/c++/7/bits/std_abs.h:56:3: error: declaration conflicts with target of using declaration already in scope
  abs(long __i) { return __builtin_labs(__i); }
  ^
/usr/include/c++/v1/stdlib.h:111:44: note: target of using declaration
inline _LIBCPP_INLINE_VISIBILITY long      abs(     long __x) _NOEXCEPT {return  labs(__x);}
                                           ^
/usr/include/c++/7/bits/std_abs.h:52:11: note: using declaration
  using ::abs;
          ^
/usr/include/c++/7/bits/std_abs.h:61:3: error: declaration conflicts with target of using declaration already in scope
  abs(long long __x) { return __builtin_llabs (__x); }
  ^
/usr/include/c++/v1/stdlib.h:113:44: note: target of using declaration
inline _LIBCPP_INLINE_VISIBILITY long long abs(long long __x) _NOEXCEPT {return llabs(__x);}
                                           ^
/usr/include/c++/7/bits/std_abs.h:52:11: note: using declaration
  using ::abs;
          ^
In file included from main.cpp:1:
In file included from /usr/include/c++/7/iostream:39:
In file included from /usr/include/c++/7/ostream:38:
In file included from /usr/include/c++/7/ios:42:
In file included from /usr/include/c++/7/bits/ios_base.h:41:
In file included from /usr/include/c++/7/bits/locale_classes.h:40:
In file included from /usr/include/c++/7/string:52:
In file included from /usr/include/c++/7/bits/basic_string.h:6352:
In file included from /usr/include/c++/7/ext/string_conversions.h:41:
/usr/include/c++/7/cstdlib:177:3: error: declaration conflicts with target of using declaration already in scope
  div(long __i, long __j) { return ldiv(__i, __j); }
  ^
/usr/include/c++/v1/stdlib.h:116:42: note: target of using declaration
inline _LIBCPP_INLINE_VISIBILITY  ldiv_t div(     long __x,      long __y) _NOEXCEPT {return  ldiv(__x, __y);}
                                         ^
/usr/include/c++/7/cstdlib:145:11: note: using declaration
  using ::div;
          ^

As a reminder, I'm literally just trying to compile Hello, World

I also tried uninstalling and re-installing Clang with the following command:

$> sudo apt-get purge --auto-remove clang
$> sudo apt-get update
$> sudo apt-get install clang

This had no effect. I'm running Ubuntu 18.04 and I have no idea what's wrong or where to start with fixing it. My build environment is in shambles.

If possible I would like to get Clang working instead of falling back to using G++, because my IDE seems to be automatically detecting Clang and using it for syntax checking. This means that every C++ program I've written has one fatal error on line one ("iostream not found") and the rest of the file goes unchecked because that first one is a fatal error.

Update (2)

I've tried installing a few more packages from the Ubuntu apt repository with no luck:

$> sudo apt-get install libc++1 libc++1-9 libc++abi1 libc++abi1-9 llvm-9 llvm-9-dev
$> clang++ -isystem /usr/include/c++/7 -isystem /usr/include/x86_64-linux-gnu/c++/7 -stdlib=libc++ main.cpp
/usr/bin/ld: cannot find -lc++
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I also tried sudo apt-get install lc++1 only to find this is an entirely unrelated package.

Update (3)

I spent several more hours trying to resolve this, installing multiple packages both from apt and from source, trying different versions of various tools, manually copying in libraries from other sources, and even hopped onto the Clang IRC and spoke to several very knowledgeable developers directly.

No one was able to figure out what's wrong with my laptop, and nothing I did ever got it working.

Unfortunately I won't still have this laptop in another two weeks, so I'll likely need to close this issue as "cannot reproduce" - because once the laptop is gone I will have no way of replicating the broken development environment.

Beliabelial answered 4/2, 2019 at 17:33 Comment(4)
How did you build or install Clang? Is it configured to use its own libc++ instead of the GCC libstdc++? What happens if you add the option -stdlib=libc++ when building your application?Almucantar
@Someprogrammerdude I think that I did sudo apt-get install clang, but to be honest I have no idea at this point. I've been using this laptop for development for two years now and I've had to install and uninstall every tool under the sun to get open source projects to build properly or to get third-party code to work right. There's no telling when Clang was installed or how it got to be on my system. How do I check if it's configured to use libc++ or stdc++?Beliabelial
@Someprogrammerdude When I use stdlib=libc++ I get a new error: cannot find -lc++abiBeliabelial
This seems to be the right "fix": askubuntu.com/questions/1449769/clang-cannot-find-iostreamBandore
O
23

I have also been troubled by this problem for a long time.You should try to delete the folder(cd /usr/lib/gcc/aarch64-linux-gnu/8). The reason why clang++ can't work is this folder doesn't contain libstdc++.a.

clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/aarch64-linux-gnu/5
Found candidate GCC installation: /usr/bin/../lib/gcc/aarch64-linux-gnu/5.5.0
Found candidate GCC installation: /usr/bin/../lib/gcc/aarch64-linux-gnu/6
Found candidate GCC installation: /usr/bin/../lib/gcc/aarch64-linux-gnu/6.5.0
Found candidate GCC installation: /usr/bin/../lib/gcc/aarch64-linux-gnu/7
Found candidate GCC installation: /usr/bin/../lib/gcc/aarch64-linux-gnu/7.3.0
Found candidate GCC installation: /usr/lib/gcc/aarch64-linux-gnu/5
Found candidate GCC installation: /usr/lib/gcc/aarch64-linux-gnu/5.5.0
Found candidate GCC installation: /usr/lib/gcc/aarch64-linux-gnu/6
Found candidate GCC installation: /usr/lib/gcc/aarch64-linux-gnu/6.5.0
Found candidate GCC installation: /usr/lib/gcc/aarch64-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc/aarch64-linux-gnu/7.3.0
Selected GCC installation: /usr/bin/../lib/gcc/aarch64-linux-gnu/7.3.0

check all the folders in /usr/lib/gcc/aarch64-linux-gnu/ clang++ will choose the last one,make sure there is the libstdc++.a in the last one

Oared answered 28/2, 2019 at 13:25 Comment(4)
This is definitely something I did not try, and may very well have solved my issues, unfortunately I'm no longer in possession of the laptop that was having the issue so I can't verify, as I mentioned in my last update. It was a work laptop and I got a new job. At my new job they don't have me writing any C++ code.Beliabelial
I was having a problem compiling an app that used clang++ that couldn't find the standard headers.ls ls /usr/lib/gcc/x86_64-linux-gnu/ showed both version 7 and 8. I ran mv /usr/lib/gcc/x86_64-linux-gnu/8 /tmpOklahoma
This is it. I would just add that the easiest remedy I found is to install the version of g++ that clang is trying to use. So, for instance, since clang "selected" gcc 7 in your example, the simplest fix that I know of is to "sudo apt install g++-7" (if on ubuntu or debian)Callup
This was the problem for me, and in my case it turned out that the gccgo package on Ubuntu 16.04 had created /usr/lib/gcc/x86_64-linux-gnu/6 without libstdc++.a. Removing that package (in my case, gccgo, gccgo-5, and gccgo-6) cleared that directory and fixed my clang++ toolchain.Pennon
K
18

Firstly find your version (path):

ls /usr/include/c++/

Output (your version may differ):

11

Then add the include paths, and replace 11 with your version e.g.:

-I/usr/include/c++/11 
-I/usr/include/x86_64-linux-gnu/c++/11

Build

This works for me on Linux (and replace 11 with your version):

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

With this simple example (main.cpp):

#include <iostream>
int main() { std::cout << "Hi\n"; }

Also, you may use CPLUS_INCLUDE_PATH (and replace 11 with your version):

export CPLUS_INCLUDE_PATH=/usr/include/c++/11:/usr/include/x86_64-linux-gnu/c++/11

Then this works:

clang++ main.cpp -o main

Run:

./main

And see:
https://superuser.com/questions/358255/bash-environment-variable-to-include-path-of-c-libraries

How to query the default include paths of clang++?

Clang doesn't see basic headers

https://askubuntu.com/questions/516801/clang-fails-to-compile-simple-hello-world-c-program

I hope this helps someone.

Kirkendall answered 11/10, 2019 at 10:2 Comment(0)
H
16

I found that clang was using the installation in /usr/lib/gcc/x86_64-linux-gnu/8 (using clang++ -v), and indeed this did not contain the file libstdc++.a. Rather than delete the whole directory as suggested by another answer, I was able to just install libstdc++-8-dev.

I'm on Ubuntu 18.04; gcc was already installed.

Heartsease answered 13/11, 2019 at 2:28 Comment(1)
This worked for me too. In my case (ubuntu 22.04), my clang++ (version 14) sensed that I have gcc 11 and gcc 12 installed and was looking for the c++ library for gcc 12 version, so I installed libstdc++-12-dev.Smokechaser
G
11

The issue often is caused by the fact that clang++ needs the headers provided by g++. It checks what version to use by looking for gcc. If there is a later version of gcc on your system without the corresponding g++, it will not find the g++ headers.

In other words, clang++ gives the error fatal error: 'iostream' file not found when:

  • you only install gcc-xx and not g++-xx
  • you upgrade gcc-xx but forget to upgrade g++-xx.

So, if you get the error, it should get fixed by installing the latest versions of both, something like:

sudo apt update
sudo apt install gcc-10 g++-10 
Gerdes answered 18/1, 2022 at 21:20 Comment(1)
this is it. Also, you can check which gcc versions it is looking for with clang++ -v. In my case it returns: "Found candidate GCC installation: ... 11" and "GCC installation: ... 12" adding clang++ -I/usr.../c++/11 fixes things (as suggested in one of the answers) and so does apt install gcc-12 and g++-12.Narial
F
3

Short version: make sure you have libstdc++ corresponding to the latest version of gcc on your system.

Everything was working fine on my system, until one day I tried to compile with clang and it blew up on not finding iostream. However g++ compiled fine.

I got into this situation on Ubuntu system because I had installed a newer version of gcc but did not install the corresponding c++ things. In my particular case I installed gcc-10 but had not installed libstdc++-10. When clang ran, it identified gcc-10 as the latest version of gcc (it identified the others too, but ignored them), and only looked in the appropriate locations for gcc-10.

To fix the problem of clang not finding iostream, I installed libstdc++-10 to correspond with where clang was already looking. I identified where clang was looking by adding a "-v" to the failing compile command and noticed this output:

ignoring nonexistent directory "/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/backward"
Fjeld answered 11/2, 2021 at 21:12 Comment(2)
This was exactly it for me. Cuda ended up installing gcc-12 without libstdc++-12-dev so clang no longer could find the stlAtaraxia
This was the case for me on fresh Mint 21.1 (Ubuntu derivative). g++ worked but clang couldn't find standard library headers. sudo apt install libstdc++-12-dev solved the problem.Bittner
J
1

Copying files fixed not found errors for headers

I fixed same error with copying libstdc++.a, libstdc++fs.a and libstdc++.so from folder 11 to 12.

I had 11 and 12 folders in /usr/lib/gcc/x86_64-linux-gnu/ , in the 11 folder there was libstdc++ files but in 12 there wasn't. I copied files from 11 to 12 with command below:

sudo cp /usr/lib/gcc/x86_64-linux-gnu/11/libstd** /usr/lib/gcc/x86_64-linux-gnu/12
  • If you want to execute this command make sure paths are correct. If not change paths for successful execute of cp command.

Also add this location to your path. Example for .bashrc :

export CPLUS_INCLUDE_PATH=/usr/include/c++/11:/usr/include/x86_64-linux-gnu/c++/11

And all errors gone, i can compile without errors, Visual Studio Code can see headers too.

I hope this helps someone.

Jabin answered 14/3, 2023 at 22:57 Comment(0)
H
0

I‘m using Ubuntu+vscode+GCC+clangd,in my view,you should add --query-driver=/your/path/to/compilers,that well help. enter image description here see https://clangd.llvm.org/troubleshooting#cant-find-standard-library-headers-map-stdioh-etc

Handknit answered 12/12, 2023 at 4:1 Comment(1)
Clangd is a language server that helps you while editing the code. But the question is about compilation errors.Shurlocke

© 2022 - 2024 — McMap. All rights reserved.