Clang doesn't see basic headers
Asked Answered
S

10

122

I've tried to compile simple hello world on Fedora 20 with Clang, and I get the following output:

d.cpp:1:10: fatal error: 'iostream' file not found

#include <iostream>

I don't have any idea how to resolve it.

Scrutiny answered 13/10, 2014 at 6:2 Comment(9)
clang++ -v your_file.cpp, make sure the right include directories are shown.Factorize
#include "..." search starts here: #include <...> search starts here: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9.1/../../../../include/c++ /usr/local/include /usr/bin/../lib/clang/3.4/include /usr/include End of search list.Scrutiny
Posting it here won't help. You have to determine yourself if the right include directories are being found by the compiler. If you compiled from source, make sure you did make install. And add the directories to your path.Factorize
How to add path to clang search paths ?Scrutiny
This problem can easily occur with clang if you change gcc versions.Exosmosis
@remyabel: iostream is a standard library header. If it does not work without any special options, the compiler is not correctly installed.Lolanthe
@sweet_sugar: The question is editable. Please, edit the full, exact output of clang++ -v your_file.cpp in it. It's difficult to read in the comment.Lolanthe
Is this the Fedora Clang package or did you compile it yourself?Dric
-std=c++11 and possibly -stdlib=libc++ included on the line?Cogitable
E
20

Point 3 solved the problem for me.

1. Had the same issue, fedora 21::clang 3.5.0:

clang++ -std=c++14 -pedantic -Wall test_01.cpp -o test_01 -v

2.

ignoring nonexistent directory "/usr/lib/gcc/i686-redhat-linux/4.9.2/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/bin/../lib/clang/3.5.0/include
 /usr/include
End of search list.
test_01.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>

3.

sudo yum install gcc-c++

4.

#include "..." search starts here:
#include <...> search starts here:
 /bin/../lib/gcc/i686-redhat-linux/4.9.2/../../../../include/c++/4.9.2
 /bin/../lib/gcc/i686-redhat-linux/4.9.2/../../../../include/c++/4.9.2/i686-redhat-linux
 /bin/../lib/gcc/i686-redhat-linux/4.9.2/../../../../include/c++/4.9.2/backward
 /usr/local/include
 /usr/bin/../lib/clang/3.5.0/include
 /usr/include
 /usr/lib/gcc/i686-redhat-linux/4.9.2/include
End of search list.
Elisavetgrad answered 23/4, 2015 at 11:3 Comment(2)
@Slizzered I've added clang++ -std=c++14 but get error: error: invalid value 'c++14' in '-std=c++14'Othelia
So that means that clang uses the system headers gcc provides over in the /usr/include directory right? Didn't know that explicitly until now regarding where clang gets its system includes or system headers.Xhosa
A
66

This is because g++ is not installed, so libstdc++ is not present.

You can install g++, or if LLVM is preferred, install LLVM libc++ and specify that you want to use it, like so:

sudo apt-get install libc++-dev
clang++ -stdlib=libc++ <rest of arguments>

You may wish to link /usr/bin/c++ to the default compiler:

ln -s /usr/bin/c++ /usr/bin/clang++-libc++

and then compile simply using

$ c++ <args_as_usual>
Amalberga answered 14/10, 2014 at 8:52 Comment(4)
I am using fedora 20. g++ and llvm are installed and libc++ also :-)Scrutiny
@Scrutiny Sorry I use Mint and do not know how Fedora packages things. On Debian, LLVM headers are in libc++-dev, which is separate from libc++ (the binary runtime lib).Amalberga
For Fedora, the packages are libcxx and libcxx-develDissension
I was using clang-15 in ubuntu 22.04 and this worked only partially for me. I was missing omp.h and with this i can find it, but now clang-15 cant find cassert :(Neolithic
N
60

One of the reason could be this: clang doesn't have its own header libraries for c++, so it is pointing towards gcc's library folder to access header files. You can check it out and confirm it by using command

clang -v

This will print its version and the selected gcc installation which will help you to know which version of gcc it is using. For me it was,

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/10
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

Now as it can be seen that it has selected gcc's 12th version which I didn't had installed and thus the error because it can't find one(don't know how it selected that version even though I haven't installed it). But the solution is to install g++ nth version. You can do that by following command where n is the version number that's selected by gcc and you're good to go.

sudo apt install g++-n

I got this answer after digging alot and found this blog clangmissingheaders

Northeaster answered 11/12, 2022 at 8:38 Comment(6)
I've been digging for a day for this! In my case, the problem was "'cstdint' file not found" when compiling godot on a machine that a week ago had no problem. Thank you very much!Thames
Perfect, that fixed it for me on Ubuntu22.04 LTSShawnna
not all heroes wear capes, this thing's been bugging me for days, even though I upgraded to emacs-29.1 from 27.1, it still didn't do it for me. upvotedPerlman
Thanks, my problem was with libsdc++ dev package not installed for version 12 of gcc...Kropotkin
helped me save hoursMas
Thank you for this diagnostic tip! I have installed gcc-14, but not g++-14, so clang-tidy selected version 14 but could not find C++ headers. Now I have installed libstdc++14-dev and everything works!Typecast
E
20

Point 3 solved the problem for me.

1. Had the same issue, fedora 21::clang 3.5.0:

clang++ -std=c++14 -pedantic -Wall test_01.cpp -o test_01 -v

2.

ignoring nonexistent directory "/usr/lib/gcc/i686-redhat-linux/4.9.2/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/bin/../lib/clang/3.5.0/include
 /usr/include
End of search list.
test_01.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>

3.

sudo yum install gcc-c++

4.

#include "..." search starts here:
#include <...> search starts here:
 /bin/../lib/gcc/i686-redhat-linux/4.9.2/../../../../include/c++/4.9.2
 /bin/../lib/gcc/i686-redhat-linux/4.9.2/../../../../include/c++/4.9.2/i686-redhat-linux
 /bin/../lib/gcc/i686-redhat-linux/4.9.2/../../../../include/c++/4.9.2/backward
 /usr/local/include
 /usr/bin/../lib/clang/3.5.0/include
 /usr/include
 /usr/lib/gcc/i686-redhat-linux/4.9.2/include
End of search list.
Elisavetgrad answered 23/4, 2015 at 11:3 Comment(2)
@Slizzered I've added clang++ -std=c++14 but get error: error: invalid value 'c++14' in '-std=c++14'Othelia
So that means that clang uses the system headers gcc provides over in the /usr/include directory right? Didn't know that explicitly until now regarding where clang gets its system includes or system headers.Xhosa
B
18

In my case, clang was using version 12 of GCC installation, but I was missing the libstdc++ for that version:

$ clang -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/10
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

Fixed it like this:

sudo apt install libstdc++-12-dev
Beka answered 23/2, 2023 at 14:24 Comment(0)
T
8

Looks like you should provide your clang build with -stdlib option. One of -stdlib=libc++ or -stdlib=libstdc++ will probably work.

There are more details on your subject:

When is it necessary to use the flag -stdlib=libstdc++?

Tooling answered 17/4, 2019 at 7:35 Comment(1)
Thanks that helped !Wycoff
H
3

-stdlib=libstdc++ solved it for me. Here is my complete tasks.json config:

{
"tasks": [
    {
        "type": "shell",
        "label": "clang++ build active file",
        "command": "clang++",
        "args": [
            "-std=c++11",
            "-stdlib=libstdc++",
            "hello.cpp",
            "-o",
            "hello.out",
            "--debug"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }
],
"version": "2.0.0"
Houk answered 22/10, 2019 at 12:57 Comment(0)
I
2

Make sure you have the libstdc++ installed that corresponds to the latest verison of gcc installed. Clang seems to identify the latest gcc installation and only look in the appropriate directories for that version.

Istanbul answered 11/2, 2021 at 21:30 Comment(1)
Thank you! I had gcc-10 en g++-9 installed on linux mint. This made clang completely dumb :/ No hint as to what the problem might be. Rather silly imho.Drysalt
H
2

I had a similar issue after updating using Pop!_OS 22.04 LTS x86_64.

OUTLINE OF ISSUE: Compilation worked just fine so gcc and g++ was working perfectly but the lsp clangd was just throwing ERROR in my editor (neovim) for lines containing anything involving iostream. Here was my path to refuge:

Created a env (environment) variable that clangd looks for called CLANGD_FLAGS and assigned it the path to my c++ 11 path and putting this into my .bashrc file like so:

code added to my .bashrc:

export CLANGD_FLAGS="-I/usr/include/c++/11"

NOTE:

  • your path may vary obviously.. Just be sure to precede your path with the -I option.
  • you can also just link the path to a compile_commands.json file if you desire more settings or specifics ex:
export CLANGD_FLAGS="--compile-commands-dir=/path/to/compile_commands.json --flag1 --flag2"

This allowed me to access <iostream> globally and avoid writing the compile_commands.json for each new program or project.

Hertford answered 21/12, 2022 at 1:44 Comment(0)
S
1

I've tried to reinstall command line tools and the "ln" command, but it still cannot be fixed.

And after trying almost every other solution on the website, this still cannot be fixed. But the thing is going to be clear: the compiler attempted to find the header files in /usr/include, but although with installed command line tools, there is no this folder at all.

Maybe the best straightforward approach for us is installing the Xcode and coding in this IDE, or other compilers, but not the most lower-cost situation.

Mac has a built-in Clang, we do not need to install extra compilers. Here are the steps.

We can check the CommandLineToos folder, if you haven't installed it, try this command to install it in advance.

xcode-select --install

In the CommandLineTools folder, we may check the route of SDKs, which is /Library/Developer/CommandLineTools/SDKs

/Library/Developer/CommandLineTools/SDKs

We may use MacOSX.sdk, for me, it is also MacOSX12.0.sdk, to find the headers. The C basic headers is found at /Library/Developer/CommandLineTools/SDKs/MacOSX12.0.sdk/usr/include. /Library/Developer/CommandLineTools/SDKs/MacOSX12.0.sdk/usr/include

But it doesn't contained with C++ basic headers, the C++ basic headers can be found at /Library/Developer/CommandLineTools/usr/include. We can find this route with command g++ -v in the terminal as well.

g++ -v

So the solution will be obvious, type the follow commands in terminal.

  1. Open the bash_profile.

    open ~/.bash_profile

  2. Add this.

     export C_INCLUDE_PATH=/Library/Developer/CommandLineTools/SDKs/MacOSX12.0.sdk/usr/include
    
     export CPLUS_INCLUDE_PATH=/Library/Developer/CommandLineTools/usr/include
    
  3. Source it.

    source ~/.bash_profile

This error will be fixed.

Sidelong answered 11/3, 2022 at 17:13 Comment(0)
P
0

TL;DR: If you don't have sudo access and are using conda, you might want to do:

conda install gxx -c conda-forge

In my case I installed clang++ using conda install clangxx thinking it would install the required dependencies but turns out I was missing libstdgxx-devel. I guess it's one of the dependencies of gxx so it gets installed as a side effect.

Presage answered 21/11, 2022 at 14:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.