Clang doesn't see basic headers

104,554

Solution 1

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.

Solution 2

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>

Solution 3

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++?

Solution 4

-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"

Solution 5

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.

Share:
104,554
sweet_sugar
Author by

sweet_sugar

I am a fanatic of programming. I'm interested in Python, Django and C++.

Updated on July 05, 2022

Comments

  • sweet_sugar
    sweet_sugar almost 2 years

    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.

  • sweet_sugar
    sweet_sugar over 9 years
    I am using fedora 20. g++ and llvm are installed and libc++ also :-)
  • ArunasR
    ArunasR over 9 years
    @sweet_sugar 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).
  • Isaac Woods
    Isaac Woods over 6 years
    For Fedora, the packages are libcxx and libcxx-devel
  • Hamed_gibago
    Hamed_gibago almost 5 years
    @Slizzered I've added clang++ -std=c++14 but get error: error: invalid value 'c++14' in '-std=c++14'
  • jxramos
    jxramos over 4 years
    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.
  • DeSpeaker
    DeSpeaker about 4 years
    Thanks that helped !
  • Jaap Versteegh
    Jaap Versteegh over 2 years
    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.