[cfe-dev] Compiling clang on Mac OS is not straight forward

Sebastian Biallas via cfe-dev cfe-dev at lists.llvm.org
Wed Oct 12 04:17:40 PDT 2016


Hi all,

I’m compiling clang (in particular clang-check) from source on Mac OS. Getting it to actually work, however, was a bit involved on my system. See below for what I did (and where either I did something work or the instructions could be improved).

I used qtcreator to run cmake and compile, which seems to work fine except that the resulting clang has some problems finding the required headers to parse anything non-trivial. It does not find, e.g., <vector>. 

Ok, so according to the "getting started" page I apparently have to set GCC_INSTALL_PREFIX to point to these headers. I seems that the C++ headers are located somewhere around /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/ on my system. This is at least where gcc (which actually is an apple-flavored clang) picks them up.

Anyway, I tried to set this (with and without /bin/ at the end), recompiled, but it did not seem to affect clang in any way. But it also did not complain.

Then I saw that the resulting clang tries to find the C++ headers first in its build directory. So I put a symbolic link there to the C++ headers on my system. Then it could find <vector> but not <string.h> (which is transitively included by vector).

Next thing I tried is, I gathered all the search directories of my compiler using:
g++ -E -x c++ - -v < /dev/null

Then, in an act of brute force, added all these directories to C_INCLUDE_DIRS for clang. This now at least had an effect, but still did not work:

/Users/biallas/prog/build-llvm-Desktop-Vorgabe/bin/../include/c++/v1/cstdint:175:8: error: no member named
      'int_fast32_t' in the global namespace
using::int_fast32_t;

At this point, I thought that maybe my system C++ headers are too old or not compatible with the newest clang. So I checked out libstdc++ as well and put it into the project directory. I removed my custom flags and recompiled. 

The resulting clang now spills out the following (slightly amusing) message:

$ /Users/biallas/prog/build-llvm-Desktop-Vorgabe/bin/clang -c move.cc 
In file included from move.cc:1:
In file included from /Users/biallas/prog/build-llvm-Desktop-Vorgabe/bin/../include/c++/v1/vector:265:
In file included from /Users/biallas/prog/build-llvm-Desktop-Vorgabe/bin/../include/c++/v1/iosfwd:90:
/Users/biallas/prog/build-llvm-Desktop-Vorgabe/bin/../include/c++/v1/wchar.h:119:15: fatal error: 
      'wchar.h' file not found
#include_next <wchar.h>

Hmm… So maybe it’s now finding the correct C++ stuff but the C stuff is still broken. I set C_INCLUDE_DIRS to
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include (because this is where gcc picks the C stuff up) and, Eureka!, it finally works.

My question now is: Isn’t C_INCLUDE_DIRS supposed to be set automatically? At some point either I messed up or the build script could be improved.

My system is a pretty standard Mac OS 10.12. The only thing that might confuse is a clang-format installed using brew.

Best,
Sebastian




More information about the cfe-dev mailing list