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.
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>
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.
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
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++?
-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"
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.
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.
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.
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.
Open the bash_profile.
open ~/.bash_profile
Add this.
export C_INCLUDE_PATH=/Library/Developer/CommandLineTools/SDKs/MacOSX12.0.sdk/usr/include
export CPLUS_INCLUDE_PATH=/Library/Developer/CommandLineTools/usr/include
Source it.
source ~/.bash_profile
This error will be fixed.
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
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.
Related
I am on Win10. I installed MinGW in Anaconda through
$ conda install -c anaconda mingw
But when I check with $ g++ -v
I see Thread model: win32, which does not support C++11 Thread class. As results when I run $ g++ myprogram.cpp -std=c++11 I got error
error: 'thread' is not a member of 'std'
Outside of Anaconda, I installed MinGW with installer from https://sourceforge.net/projects/mingw-w64/
When installing, choose "posix", and I got what I need.
So my question is, how to make this happen in Anaconda? I want to either
Set Anaconda's C++ compiler to the one outside (the one I installed with installer)
or, install a MinGW with posix in Anaconda.
I tried to find solutions online, the following helped me sorted out my questions. But I couldn't find a solution yet.
Troubles with POSIX program using threads with gcc
https://coderwall.com/p/rzkw6q/compile-c-code-with-c-11-threads
https://docs.conda.io/projects/conda-build/en/latest/resources/compiler-tools.html
The full output is as below. I appreciate for your help!
$ g++ -v
Using built-in specs.
COLLECT_GCC=C:\ProgramData\Anaconda3\Scripts\g++.bat\..\..\MinGW\bin\g++.exe
COLLECT_LTO_WRAPPER=c:/programdata/anaconda3/mingw/bin/../libexec/gcc/x86_64-w64-mingw32/4.7.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../../../build/gcc/src/configure --target=x86_64-w64-mingw32 --prefix=/c/bb/vista64-mingw32/mingw-x86-x86_64/build/build/root --with-sysroot=/c/bb/vista64-mingw32/mingw-x86-x86_64/build/build/root --enable-languages=all,obj-c++ --enable-fully-dynamic-string --disable-multilib
Thread model: win32
gcc version 4.7.0 20111220 (experimental) (GCC)
After installing clang++ (tried v. 6.0.1 and 7.0) with:
brew install --with-toolchain llvm
very trivial programs result to the following error:
In file included from test.cpp:1:
In file included from /usr/local/Cellar/llvm/7.0.0/include/c++/v1/iostream:38:
In file included from /usr/local/Cellar/llvm/7.0.0/include/c++/v1/ios:215:
In file included from /usr/local/Cellar/llvm/7.0.0/include/c++/v1/iosfwd:90:
/usr/local/Cellar/llvm/7.0.0/include/c++/v1/wchar.h:119:15: fatal error: 'wchar.h' file not found
#include_next <wchar.h>
Command used to compile:
clang++7() {
LDFLAGS="-L/usr/local/opt/llvm/lib -Wl,-rpath,/usr/local/opt/llvm/lib"
CPPFLAGS="-I/usr/local/opt/llvm/include"
/usr/local/opt/llvm/bin/clang++ -std=c++11 $CPPFLAGS $LDFLAGS $1
}
Is it possible to use the official clang instead of Apple's version?
With Apple's version, we do not even know which version of LLVM it really is...
It appears that as of Mojave (10.14), Xcode doesn't install system headers in /usr/include anymore. There is a compatibility package that does, but it's not recommended.
Instead, the official solution is for tools to search for headers in /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk. That path can be obtained from
xcrun --show-sdk-path
The release notes say
The Command Line Tools package installs the macOS system headers inside the macOS SDK. Software that compiles with the installed tools will search for headers within the macOS SDK provided by either Xcode at:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk
or the Command Line Tools at:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
depending on which is selected using xcode-select.
If you built clang yourself, this can be achieved by passing the -isysroot option to clang:
clang++ -isysroot "$(xcrun --show-sdk-path)" …
See also: https://github.com/Homebrew/homebrew-core/issues/32765
It works for me to add a -I (minus eye) option to the clang++ command line, pointing to /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include
$ gcc 12.c -fopenmp
12.c:9:9: fatal error: 'omp.h' file not found
#include<omp.h>
^
1 error generated.
While compiling openMP programs I get the above error. I am using OS X Yosemite. I first tried by installing native gcc compiler by typing gcc in terminal and later downloaded Xcode too still I got the same error. Then I downloaded gcc through:
$ brew install gcc
Still I'm getting the same error. I did try changing the compiler path too still it shows:
$ which gcc
/usr/bin/gcc
So how do I compile programs with gcc?
EDIT: As of 13 Aug 2017 the --without-multilib option is no longer present in Homebrew and should not be used. The standard installation
brew install gcc
will provide a gcc installation that can be used to compile OpenMP programs. As below it will be installed into /usr/local/bin as gcc-<version>. The current gcc version available from Homebrew (as of writing) will install as gcc-8. You can compile programs with OpenMP support using it via
gcc-8 -fopenmp hello.c
Alternatively you could put an alias in your .bashrcfile as
alias gcc='gcc-8'
and then compile using
gcc -fopenmp hello.c
Note: I'm leaving the original post here in case it is useful to somebody.
The standard gcc available on OS X through XCode and Clang doesn't support OpenMP. To install the Homebrew version of gcc with OpenMP support you need to install it with
brew install gcc --without-multilib
or as pointed out by #Mark Setchell
brew reinstall gcc --without-multilib
This will install it to the /usr/local/bin directory. Homebrew will install it as gcc-<version> so as not to clobber the gcc bundled with XCode.
I finally did some research and I finally came across a solution here: <omp.h> library isn't found in the GCC version (4.2.1) in Mavericks.
I got a new gcc complier from http://hpc.sourceforge.net/
Then I placed a new executable folder by
$ sudo tar -xvf gcc-4.9-bin.tar -C /
Later I switched to it by
export PATH=/usr/local/bin:$PATH that seemed to do the trick!
My OS is OS X 10.10.2 and the default compiler for C is clang.
But this version of clang does not support ubsan (undefined sanitizer) which comes in the 3.4 release of clang. I also want to use KLEE to do some analysis. AFAIK KLEE works well with LLVM-<=3.4. I decided to install
llvm-3.4 and clang-3.4 in my laptop.
After installing clang-3.4 in my system, I encountered a issue that the compiler can not locate the c++ header file. I installed clang-3.4 in /usr/local and I can find the c++ header file in /usr/local/include/c++/4.8.4. How can I add this directory to the search path of clang-3.4 and also the c++ library?
for the following demo code:
#include <iostream>
using namespace std;
int main(){
cout<<"Hellow World\n";
return 0;
}
When I compile it using command clang++ test.cpp, I got the error
test1.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
Below is the version of clang I used
clang version 3.4 (tags/RELEASE_34/final)
Target: x86_64-apple-darwin14.1.0
Thread model: posix
I used the following shell command to install llvm-3.4 and clang-3.4:
wget http://llvm.org/releases/3.4/llvm-3.4.src.tar.gz \
http://llvm.org/releases/3.4/clang-3.4.src.tar.gz \
http://llvm.org/releases/3.4/clang-tools-extra-3.4.src.tar.gz \
http://llvm.org/releases/3.4/compiler-rt-3.4.src.tar.gz
tar zxf llvm-3.4.src.tar.gz
tar zxf clang-3.4.src.tar.gz -C llvm-3.4/tools
mv llvm-3.4/tools/clang{-3.4,}
tar zxf clang-tools-extra-3.4.src.tar.gz -C llvm-3.4/tools/clang/tools
mv llvm-3.4/tools/clang/tools/{clang-tools-extra-3.4,extra}
tar zxf compiler-rt-3.4.src.tar.gz -C llvm-3.4/projects
mv llvm-3.4/projects/compiler-rt{-3.4,}
cd llvm-3.4
./configure --enable-cxx11 \
--enable-bindings=none --enable-shared \
--enable-debug-symbols --enable-optimized
make
make install
Now I have two versions of clang in my OS, one is the default one shipped with OSX located in /usr/bin and the other is clang-3.4 located in /usr/local/bin. The previous one can find the C++ header file while the latter can not.
Did you read the user documentation of clang notably the section on command line options ? BTW, current (march 2015) version of clang is 3.6!
For C++ code you should use the clang++ command, not the clang command.
You might pass -I and -L options to clang++ but you might perhaps have misinstalled your clang compiler.
You should be aware of the -v and -H options of clang or clang++ ; they could be useful, at least to understand more your issue.
addenda
BTW, a program reported to work with Clang 3.4 is extremely likely to work with a more modern version, like Clang 3.5 or 3.6
You probably have a PATH issue; you should have configure -d your Clang-3.4 & LLvm-3.4 programs with --program-suffix=-my-3.4 (if you do that, repeat your entire compiler build and installation) and you probably should run /usr/local/bin/clang++-my-3.4 ; maybe you also need some --with-gcc-toolchain additional configure option.
I'm pretty sure that you should be able to try to compile or use your mysterious software requiring Clang-3.4 with a more modern version like 3.5 or 3.6 ; your MacOSX 10.10.2 is rumored to have a Clang-3.5 based system compiler, it very probably is able to compile and work with your mysterious software.
I have Xcode 4.5.2 on Moutain Lion, and I have install the lastest "Command Line Tools" but when I tried to compile with g++ or clang++ (and the options -std=c++11 -stdlib=libc++) I get an error.
With g++:
cc1plus: error: unrecognized command line option "-std=c++11"
cc1plus: error: unrecognized command line option "-stdlib=libc++"
With clang++:
clang: error: invalid deployment target for -stdlib=libc++ (requires OS X 10.7 or later)
It's in a Qt project.
So how can I used the C++11 on my Mac ?
As you found, g++ does not support those command line options.
It sounds like you're using Xcode.
For clang, you should look at the project settings, and make sure that the "Deployment Target" is set to 10.7 (or 10.8)
What the error message is telling you is that libc++ is not available for 10.6 and before.
I installed gcc-4.7 on my Mac to make C++11 work. GCC in its current version is fairly good at supporting C++11, so this should be a fair choice.
The installation can be done by Homebrew and is not that complicated (at least I was able to do it...)
To install Homebrew if you do not already have it:
ruby -e "$(curl -fsSkL raw.github.com/mxcl/homebrew/go)"
Now run
brew doctor
and fix whatever problems come up (there is something written in the hombrew documentation for that). Finally, install current gcc:
brew install gcc
If everything goes well you should be able to access g++-4.7, which allows -std=c++0x.
Try -std=c++0x if c++11 doesn't work. Support for the -std=c++11 option is relatively new in GCC and you might not have a recent enough version.
I'd trust Marshall on the libc++ issue.