CLion on macOS: SDK selection leads to unexpected header usage
Problem
When building a CMake project in CLion on macOS, the compilation unexpectedly picks up headers from /usr/local/include (for example, OpenSSL 3.0 headers) even though this directory is not present in the compiler command line arguments. Linking to OpenSSL 1.1 can lead to symbol mismatches and build errors due to the wrong header versions being used.
Cause
Recent CLion versions bundle CMake newer than 4.0.
As of CMake 4.0 (see the release notes), CMAKE_OSX_SYSROOT defaults to empty, deferring to the compiler's default macOS SDK selection. Also, on macOS with Ninja Generators and Makefile Generators, when a compiler is found in /usr/bin, it is now used as-is and is no longer mapped to the corresponding compiler inside Xcode.
This may result in system include paths such as /usr/local/include being prioritized, leading to the wrong headers being used.
Resolution
Override the CMAKE_OSX_SYSROOT variable in your CMake project to explicitly point to the Xcode SDK. For example, add the following line to your CMakeLists.txt (after the project() call):
set(CMAKE_OSX_SYSROOT "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk")Alternatively, you can try using a custom CMake older than 4.0 instead of the bundled one.
Please sign in to leave a comment.