Search code examples
c++macoscmake

CMake for macOS. How do I set the isysroot variable according to my default Xcode framework


In my CMake based macOS project, the parameter CMAKE_OSX_SYSROOT is not set and I expect to get the default framework which is also used in Xcode builder :

i.e. /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk

However, the default value is instead macosx and in the build command cannot link it to the default sysroot, due to which I get the following compilation error c++: warning: no such sysroot directory: 'macosx' [-Wmissing-sysroot]

Any idea how should I make CMake choose the default sysroot?


Solution

  • According to the documentation CMAKE_OSX_SYSROOT it is set (if not set explicitly):

    Note also this should be set before project!

    Here is my attempt to reproduce your issue and respecting points from documentation:

    CMakeLists.txt:

    cmake_minimum_required(VERSION 3.28)
    
    set(CMAKE_OSX_DEPLOYMENT_TARGET 11)
    
    project(MyTestSysRoot LANGUAGES CXX)
    
    add_executable(test_app main.cpp)
    
    message(STATUS "CMAKE_OSX_SYSROOT=${CMAKE_OSX_SYSROOT}")
    

    It works without problems:

    % set | grep SDKROOT
    % cmake --version
    cmake version 3.31.0
    
    CMake suite maintained and supported by Kitware (kitware.com/cmake).
    % cmake -G Xcode -S . -B build
    -- The CXX compiler identification is AppleClang 16.0.0.16000026
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ - skipped
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- CMAKE_OSX_SYSROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk
    -- Configuring done (11.8s)
    -- Generating done (0.0s)
    -- Build files have been written to: /Users/me/repos/stackoverflow/cmake_macos_sysroot/build
    
    % cmake -LA build | grep CMAKE_OSX_SYSROOT
    -- CMAKE_OSX_SYSROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk
    CMAKE_OSX_SYSROOT:PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk
    

    Check your CMakeLists.txt if it respects all rules mention in documentation.