Xcode: CMAKE_CXX_COMPILER is ignored thus unable compile with different compilers
Please below minimal example: CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(sample)
add_executable(macos-sample main.cpp)
add_subdirectory(ios)
ios/CMakeLists.txt
set(CMAKE_CXX_COMPILER <path-to-ios-compiler>)
add_executable(ios-sample ../main.cpp)
This minimal example works perfectly if I generate a make
project, (e.g. cmake ..
) the macos-sample
target is compiled with the macos compiler, and the ios-sample
target is compiled with the ios compiler. However, if I generate a xcode
project, (e.g. cmake -G Xcode ..
), both the macos-sample
target and the ios-sample
target are compiled with the macos compiler. After further trial and error, I discovered that Xcode project seems to decide which compiler to use based on CMAKE_OSX_SYSROOT
. However, the problem with CMAKE_OSX_SYSROOT
is that setting it seem to only work if it's being set on the head CMakeLists.txt, meaning the below:
CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(sample)
set(CMAKE_OSX_SYSROOT <path-to-ios-sdk>)
add_executable(macos-sample main.cpp)
add_subdirectory(ios)
ios/CMakeLists.txt
add_executable(ios-sample ../main.cpp)
both the macos-sample
target and the ios-sample
target are compiled with the ios compiler, but the below:
CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(sample)
add_executable(macos-sample main.cpp)
add_subdirectory(ios)
ios/CMakeLists.txt
set(CMAKE_OSX_SYSROOT <path-to-ios-sdk>)
add_executable(ios-sample ../main.cpp)
both the macos-sample
target and the ios-sample
target are still compiled with the macos compiler. Can we fix Xcode so that it can recognize CMAKE_CXX_COMPILER
just like make
so that it's possible to compile the macos-sample
target with macos compiler and compile the ios-sample
target with ios compiler on Xcode?