clang++ build from source is not able to find C++ headers

Hello,

I am running OS X, when I build clang++ from source and use it to compile .cpp files it fails as it is not able to find C++ header. I am not building libc++ along with llvm and clang.

using -stdlib=libstdc++ solves problem partially but it fails when using C++11 threads, again it can’t find , to get this work clang++ should work with -stdlib=libc++ but it fails with previous error of not finding C++ headers.

Is it required to build libc++ along with clang++?

clang++ with Xcode-command line tool works fine and it also work with -stdlib=libc++.

How to solve this problem with my build clang++?

Sincerely,
Vivek

I am running OS X, when I build clang++ from source and use it to compile
.cpp files it fails as it is not able to find C++ header. I am not building
libc++ along with llvm and clang.

When I'm not building with libcxx in-tree I find it easiest to just
symlink Xcode's libc++ headers into the build directory. Something
like "ln -s /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++
build/include".

using -stdlib=libstdc++ solves problem partially but it fails when using
C++11 threads, again it can't find <thread>, to get this work clang++ should
work with -stdlib=libc++ but it fails with previous error of not finding C++
headers.

Yes, the libstdc++ included with OS X is ancient (from GCC 4.2 I
think) and there only for compatibility reasons. It has virtually no
support for C++11, which LLVM needs.

Cheers.

Tim.

> I am running OS X, when I build clang++ from source and use it to compile
> .cpp files it fails as it is not able to find C++ header. I am not
building
> libc++ along with llvm and clang.

When I'm not building with libcxx in-tree I find it easiest to just
symlink Xcode's libc++ headers into the build directory. Something
like "ln -s
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++
build/include".

Thanks Tim,

symlink to build include does not help because clang++ looks headers into
build/lib/clang/3.9.0/include ( as per output of clang++ -v) but I symlink
to /usr/local/include , that worked I hope there is nothing wrong in doing
so.

-Vivek

    > I am running OS X, when I build clang++ from source and use it to compile
    > .cpp files it fails as it is not able to find C++ header. I am not building
    > libc++ along with llvm and clang.

    When I'm not building with libcxx in-tree I find it easiest to just
    symlink Xcode's libc++ headers into the build directory. Something
    like "ln -s
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++
    build/include".

Thanks Tim,

symlink to build include does not help because clang++ looks headers
into build/lib/clang/3.9.0/include ( as per output of clang++ -v) but I

That path is where clang is looking for compiler-provided headers. You should see another path in the -v output that says where it is looking for the c++ headers, which should not be in the same place.

Jon

Hi Vivek,

you can also include these lines below to your ~/.bash_profile:

LLVM_BUILD="/Developer/llvm/build" # Path to your build directory

alias new-clang="$LLVM_BUILD/bin/clang -Wno-expansion-to-defined -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include"
alias new-clang++="$LLVM_BUILD/bin/clang++ -Wno-expansion-to-defined -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/c++"

where you only need to set LLVM_BUILD to where in you system you built llvm/clang.

After that, invoke “source ~/.bash_profile” on the current terminal, or simply open a new terminal, and you’ll be able to use new-clang and new-clang++, instead of clang and clang++, to invoke your newest compiled binary.

Hi Vivek,

you can also include these lines below to your ~/.bash_profile:

LLVM_BUILD="/Developer/llvm/build" # Path to your build directory

alias new-clang="$LLVM_BUILD/bin/clang -Wno-expansion-to-defined
-I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include"
alias new-clang++="$LLVM_BUILD/bin/clang++ -Wno-expansion-to-defined
-I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/c++"

where you only need to set LLVM_BUILD to where in you system you built
llvm/clang.

After that, invoke "source ~/.bash_profile" on the current terminal, or
simply open a new terminal, and you'll be able to use new-clang and
new-clang++, instead of clang and clang++, to invoke your newest compiled
binary.

Thanks for this neat suggestion.

-Vivek

--
Cristianno Martins
PhD Student of Computer Science
University of Campinas
cmartins@ic.unicamp.br
<cristiannomartins@hotmail.com>

    > I am running OS X, when I build clang++ from source and use it to
compile
    > .cpp files it fails as it is not able to find C++ header. I am not
building
    > libc++ along with llvm and clang.

    When I'm not building with libcxx in-tree I find it easiest to just
    symlink Xcode's libc++ headers into the build directory. Something
    like "ln -s

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++
    build/include".

Thanks Tim,

symlink to build include does not help because clang++ looks headers
into build/lib/clang/3.9.0/include ( as per output of clang++ -v) but I

That path is where clang is looking for compiler-provided headers. You
should see another path in the -v output that says where it is looking for
the c++ headers, which should not be in the same place.

Ok I understood.

Thanks,
Vivek

I believe the right way is to use -isysroot with the path to the SDK:

clang++ -isysroot xcrun --show-sdk-path

Otherwise I always end-up having other issues with missing system/platform headers.

Hi Vivek,

you can also include these lines below to your ~/.bash_profile:

LLVM_BUILD="/Developer/llvm/build" # Path to your build directory

alias new-clang="$LLVM_BUILD/bin/clang -Wno-expansion-to-defined
-I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include"
alias new-clang++="$LLVM_BUILD/bin/clang++ -Wno-expansion-to-defined
-I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/c++"

where you only need to set LLVM_BUILD to where in you system you built
llvm/clang.

After that, invoke "source ~/.bash_profile" on the current terminal, or
simply open a new terminal, and you'll be able to use new-clang and
new-clang++, instead of clang and clang++, to invoke your newest compiled
binary.

I believe the right way is to use -isysroot with the path to the SDK:

clang++ -isysroot `xcrun --show-sdk-path`

Otherwise I always end-up having other issues with missing system/platform
headers.

Can you put this in http://clang.llvm.org/docs/FAQ.html ? I've definitely
seen a bunch of people ask this.

-- Sean Silva

Hi Vivek,

you can also include these lines below to your ~/.bash_profile:

LLVM_BUILD="/Developer/llvm/build" # Path to your build directory

alias new-clang="$LLVM_BUILD/bin/clang -Wno-expansion-to-defined
-I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include"
alias new-clang++="$LLVM_BUILD/bin/clang++ -Wno-expansion-to-defined
-I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/c++"

where you only need to set LLVM_BUILD to where in you system you built
llvm/clang.

After that, invoke "source ~/.bash_profile" on the current terminal, or
simply open a new terminal, and you'll be able to use new-clang and
new-clang++, instead of clang and clang++, to invoke your newest compiled
binary.

I believe the right way is to use -isysroot with the path to the SDK:

clang++ -isysroot `xcrun --show-sdk-path`

Otherwise I always end-up having other issues with missing system/platform
headers.

This gives an error for me. Any ideas?

Sean:~/pg/llvm % ~/pg/release/bin/clang++ -v -isysroot `xcrun
--show-sdk-path` -fsyntax-only test.cpp
clang version 3.9.0 (trunk 274185) (llvm/trunk 274183)
Target: x86_64-apple-darwin14.5.0
Thread model: posix
InstalledDir: /Users/Sean/pg/release/bin
 "/Users/Sean/pg/release/bin/clang-3.9" -cc1 -triple
x86_64-apple-macosx10.11.0 -Wdeprecated-objc-isa-usage
-Werror=deprecated-objc-isa-usage -fsyntax-only -disable-free
-disable-llvm-verifier -discard-value-names -main-file-name test.cpp
-mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim
-masm-verbose -munwind-tables -target-cpu core2 -target-linker-version
253.9 -v -dwarf-column-info -debugger-tuning=lldb -resource-dir
/Users/Sean/pg/release/bin/../lib/clang/3.9.0 -isysroot
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk
-stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir
/Users/Sean/pg/llvm -ferror-limit 19 -fmessage-length 158 -stack-protector
1 -fblocks -fobjc-runtime=macosx-10.11.0 -fencode-extended-block-signature
-fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option
-fcolor-diagnostics -x c++ test.cpp
clang -cc1 version 3.9.0 based upon LLVM 3.9.0svn default target
x86_64-apple-darwin14.5.0
ignoring nonexistent directory
"/Users/Sean/pg/release/bin/../include/c++/v1"
ignoring nonexistent directory
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/c++/v1"
ignoring nonexistent directory
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/local/include"
ignoring nonexistent directory
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /Users/Sean/pg/release/bin/../lib/clang/3.9.0/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks
(framework directory)
End of search list.
test.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
         ^
1 error generated.
zsh: exit 1     ~/pg/release/bin/clang++ -v -isysroot `xcrun
--show-sdk-path` -fsyntax-only

-- Sean Silva

Clang expects libc++ headers to be in /path/to/clang/../include/c++ on
OS X because they're provided with the toolchain rather than the SDK.
Either symlink them there, or build Clang with libc++ (I tend to
symlink).

Cheers.

Tim.

A build flag or a command line flag would be nice though.

I think I build with libc++ locally, but I still need to pass isysroot for some system headers though.

Thanks for the info.