76

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.

Community
  • 1
  • 1
sweet_sugar
  • 1,052
  • 2
  • 11
  • 20
  • 2
    `clang++ -v your_file.cpp`, make sure the right include directories are shown. –  Oct 13 '14 at 06:03
  • #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. – sweet_sugar Oct 13 '14 at 06:11
  • 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. –  Oct 13 '14 at 06:13
  • How to add path to clang search paths ? – sweet_sugar Oct 13 '14 at 06:30
  • This problem can easily occur with clang if you change gcc versions. – o11c Oct 13 '14 at 06:57
  • 1
    @remyabel: `iostream` is a standard library header. If it does not work without any special options, the _compiler_ is not correctly installed. – Jan Hudec Oct 14 '14 at 08:42
  • @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. – Jan Hudec Oct 14 '14 at 08:43
  • Is this the Fedora Clang package or did you compile it yourself? – rubenvb Oct 14 '14 at 08:43
  • 1
    `-std=c++11` and possibly `-stdlib=libc++` included on the line? – firda Oct 14 '14 at 08:44

7 Answers7

51

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>
ArunasR
  • 1,737
  • 1
  • 13
  • 15
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.
Slizzered
  • 849
  • 2
  • 9
  • 23
user4823890
  • 216
  • 2
  • 2
  • @Slizzered I've added clang++ -std=c++14 but get error: error: invalid value 'c++14' in '-std=c++14' – Hamed_gibago May 18 '19 at 12:57
  • 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. – jxramos Dec 12 '19 at 23:00
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++?

1

-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"
eyesfree
  • 96
  • 2
  • 5
1

For anyone Googling this & have a MacOS, here's the solution I found:

Add the following to your .bashrc/.zshrc file

export CPLUS_INCLUDE_PATH=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1

This should fix it.

Eddy Ekofo
  • 461
  • 5
  • 11
1

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.

ddiepo
  • 71
  • 2
  • 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. – Jaap Versteegh Oct 07 '21 at 18:28
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.