How to find and provide C++ library headers for clang?

Viewed 53

I've built LLVM and Clang from sources using following instruction in order to try some of the latest C++ features.

When I try to compile basic C++ program using this clang I get errors about missing basic headers:

% /usr/local/bin/clang++ -std=c++20 main.cpp
In file included from main.cpp:1:
main.cpp:3:10: fatal error: 'array' file not found
#include <array>

I similarly have brew installed clang, which works perfectly.

The instruction mentions providing C++ library headers to clang, but I don't understand:

  1. How to locate those?
  2. How to make sure, that they are also up to date to support latest C++ features?

I have MacOS Monterey 12.4

1 Answers

You should specify a path to the sdk for your target platform.

You can retreive this by relying on xcrun:

/usr/local/bin/clang++ -isysroot $(xcrun --show-sdk-path) -std=c++20 main.cpp

In case you want to target other platforms (in the example bellow, for iphone) you have to specify both the target triple and the path to the appropiate sdk:

/usr/local/bin/clang++ -target arm64-apple-ios -isysroot $(xcrun --sdk iphoneos --show-sdk-path) -std=c++20 main.cpp

There is also an alternative to the -isysroot option. You could set up the SDKROOT environment variable to point to the target sdk path:

export SDKPATH=$(xcrun --show-sdk-path)
/usr/local/bin/clang++ -std=c++20 main.cpp
Related