[MacOS 10.14.5, Xcode 10.2, CLI Tools, brew clang-9/clang++]

For some reason I am no longer able to build a simple hello world program using clang++ main.cpp. Instead, I am getting an error:

clang++ main.cpp
In file included from main.cpp:9:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/iostream:38:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/ios:215:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/iosfwd:90:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/wchar.h:119:15: fatal error: 'wchar.h' file not found
#include_next <wchar.h>

I installed the homebrew's llvm yesterday because Apple's version does not support the -fopenmp flag correctly. I'm wondering if installing the 2nd version of clang may have changed something I didn't notice and now I can't figure out.

Two things strike me here;
1. why can't wchar.h find itself in the same directory that it is already at? (Note that it fails on the #include_next, which is inside the header file it supposedly can't find.)
2. Why is Xcode using XcodeDefault.xctoolchain showing instead of MacOSX10.14.sdk.

I've read countless posts about how to fix this using xcode-select but unfortunately, it doesn't.

Nonetheless, I have tried

sudo xcode-select /Applications/Xcode.app

and reinstalled my commandline tools, but to no avail.

xcrun --show-sdk-path 

returns the correct path:


One answer suggested setting an environment variable in my bash profile, but since I didn't need it in the past, I don't like that option.

Whilst clang++ main.cpp doesn't work, I am able to build using any of the following:

xcrun clang++ main.cpp
/usr/local/opt/llvm/bin/clang++ main.cpp
g++ main.cpp  (This execute's Apple's version)
/usr/local/bin/g++-9 main.cpp
I can also build/run from within an Xcode (GUI) project (CLI Tool)

Any suggestions?

  • sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer Run this apple.stackexchange.com/questions/216573/… Are you using Cmake GUI? – anki Jan 26 '20 at 13:47
  • Unfortunately, that didn't work. And no, I'm not using the cmake gui here. I'm just trying to use clang to compile/link the hello world program. The real reason for wanting this to work is that pip2 install gdbgui fails due to a clang error, and I don't know how to tell pip2 to use a different compiler, not that I really should have to. – gone Jan 26 '20 at 14:36
  • Why should I use cmake? clang++ main.cpp should produce a.out. That's all I want. – gone Jan 26 '20 at 15:00
  • Same question here: stackoverflow.com/questions/46342411/wchar-h-file-not-found – Yoan Jan 27 '20 at 14:09
  • 1
    The reason for the error is because MacOS Mojave does not have a /usr/include folder and clang++ default sysroot is /. So it looks like I have to specify sysroot everytime I want to invoke Apple's clang++ or clang but not for gcc and g++. The other compilers are correctly configured to work with their default sysroots. – gone Jan 27 '20 at 15:47

You must log in to answer this question.

Browse other questions tagged .