iOS: Single Xcode project cannot build for native and Simulator
After updating to CMake 3.19 (from 3.18), I can no longer switch between building an iOS project for device and simulator.
I have created a sample project to demonstrate the issue: https://github.com/stoffera/cmake319issue
In-short: CMake 3.19 adds the absolute framework path to the project target settings. So the simulator build, will try to link against the native device libraries. 3.18 did not add the full path to either Framework Search Path or Other Linker Flags.
So after generating an Xcode project, you can build only for the scheme (device/simulator) provided in the generator step. Changing the build scheme in Xcode's drop down menu, will result in linker errors.
Here is the linker output in an Apple Silicon Mac, when building for iOS Simulator:
Ld /Users/name/projects/cmake319issue/build/Debug-iphonesimulator/Demo.app/Demo normal (in target 'Demo' from project 'LinkDemo')
cd /Users/name/projects/cmake319issue
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -target arm64-apple-ios14.4-simulator -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator14.4.sdk -L/Users/name/projects/cmake319issue/build/Debug-iphonesimulator -F/Users/name/projects/cmake319issue/build/Debug-iphonesimulator -F/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.4.sdk/System/Library/Frameworks -filelist /Users/name/projects/cmake319issue/build/LinkDemo.build/Debug-iphonesimulator/Demo.build/Objects-normal/arm64/Demo.LinkFileList -dead_strip -Xlinker -object_path_lto -Xlinker /Users/name/projects/cmake319issue/build/LinkDemo.build/Debug-iphonesimulator/Demo.build/Objects-normal/arm64/Demo_lto.o -Xlinker -no_deduplicate -Xlinker -objc_abi_version -Xlinker 2 -fobjc-link-runtime -Wl,-headerpad_max_install_names -lobjc -Xlinker -sectcreate -Xlinker __TEXT -Xlinker __entitlements -Xlinker /Users/name/projects/cmake319issue/build/LinkDemo.build/Debug-iphonesimulator/Demo.build/Demo.app-Simulated.xcent -framework CoreGraphics -framework Foundation -Xlinker -no_adhoc_codesign -Xlinker -dependency_info -Xlinker /Users/name/projects/cmake319issue/build/LinkDemo.build/Debug-iphonesimulator/Demo.build/Objects-normal/arm64/Demo_dependency_info.dat -o /Users/name/projects/cmake319issue/build/Debug-iphonesimulator/Demo.app/Demo
ld: building for iOS Simulator, but linking in .tbd built for iOS, file '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.4.sdk/System/Library/Frameworks/Foundation.framework/Foundation.tbd' for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
If running on an Intel Mac, the linker will complain about missing symbols, since it ignore ARM64 symbols, when building for x86_64.
My system:
OS: macOS 11.2
CMake: 3.19.4
Xcode: 12.4