Can't compile a basic C++ example after building successfully LLVM on Apple M1 architecture

I’ve managed to compile successfully LLVM and libc++. I want to use the compiled version as my leading toolchain to compile my codebase on macOS, as the default version of Apple Clang is multiple versions behind.

I’ve compiled the LLVM tooling and libc++ with the following commands:

$ cmake -G Ninja -S llvm -B build -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra"  -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind"  -DLLVM_DEFAULT_TARGET_TRIPLE="arm64-apple-darwin22.6.0"
$ ninja -C build runtimes
$ ninja -C build check-runtimes
$ ninja -C build install-runtimes

The binaries seem to work as expected; I’ve tested clang-format and clangd.

When I try to compile a simple example in C++:

#include <vector>

int main() {
	return 0;
}

I’m getting some strange compilation errors:

error: /usr/local/include/c++/v1/stdlib.h:148:34: error: unknown type name 'ldiv_t'
  148 | inline _LIBCPP_INLINE_VISIBILITY ldiv_t div(long __x, long __y) _NOEXCEPT {
      |                                  ^
/usr/local/include/c++/v1/stdlib.h:149:12: error: no member named 'ldiv' in the global namespace
  149 |   return ::ldiv(__x, __y);
      |          ~~^
/usr/local/include/c++/v1/stdlib.h:152:34: error: unknown type name 'lldiv_t'
  152 | inline _LIBCPP_INLINE_VISIBILITY lldiv_t div(long long __x,
      |                                  ^
/usr/local/include/c++/v1/stdlib.h:154:12: error: no member named 'lldiv' in the global namespace
  154 |   return ::lldiv(__x, __y);
      |          ~~^
In file included from main.cpp:1:
In file included from /usr/local/include/c++/v1/vector:310:
In file included from /usr/local/include/c++/v1/__algorithm/remove.h:12:
In file included from /usr/local/include/c++/v1/__algorithm/find.h:25:
In file included from /usr/local/include/c++/v1/cwchar:114:

I’ve tried to compile that same codebase using the pre-built clang-16 from homebrew and using the compiled libc++ passing the flags:

-stdlib=libc++ -nostdinc++ -I/usr/local/include/c++/v1/ -L/usr/local/lib -Wl,-rpath,/usr/local/lib

And everything is compiling just fine.

I’m assuming that the clang++ binary I built is compiled with different compilation flags.

Is anyone familiar with this issue?

1 Like

https://stackoverflow.com/questions/51761599/cannot-find-stdio-h
The above should fix your problem.

1 Like

I have encountered the same problem, and solved by setting SDKROOT. Maybe this should be dealed within LLVM itself.