dyld: Library not loaded: framework_test.framework/framework_test ... Reason: image not found
At first glance it duplication of this one, but seems reasons are different.
I have simple framework:
cmake_minimum_required (VERSION 3.17)
project(CheckSystemVersion)
set (CMAKE_Swift_LANGUAGE_VERSION 5)
set (CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED NO)
set (CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
enable_language (Swift)
add_library (framework_test SHARED dummy.swift)
set_target_properties (framework_test PROPERTIES
FRAMEWORK TRUE
FRAMEWORK_VERSION A
MACOSX_FRAMEWORK_IDENTIFIER "framework.test"
VERSION "1.0"
SOVERSION 1.0
MACOSX_BUNDLE TRUE
)
install(TARGETS framework_test FRAMEWORK DESTINATION .)
I want to build it for iOS (for example for simulator). According to documentation I do it like this:
cmake -DBUILD_SHARED_LIBS=ON \
-DCMAKE_SYSTEM_NAME=iOS \
-DCMAKE_OSX_SYSROOT=iphonesimulator \
-DCMAKE_OSX_ARCHITECTURES=x86_64 \
-DCMAKE_OSX_DEPLOYMENT_TARGET=9.3 \
-DCMAKE_INSTALL_PREFIX=dist \
-G Xcode \
..
cmake --build . --target install
Then I add it to my Xcode application (which is NOT generated by Xcode). When I compile and launch my application I have runtime error:
dyld: Library not loaded: framework_test.framework/framework_test
Referenced from: /Users/xxx/Library/Developer/CoreSimulator/Devices/5A010813-A5D2-4B2F-A422-BB3B673F66A7/data/Containers/Bundle/Application/1570F317-817B-4672-89FD-34BC196C8D0A/test_app.app/test_app
Reason: image not found
After checking different customs toolchains I found that many of them specify CMAKE_SYSTEM_VERSION
.
Indeed in documentation for CMAKE_SYSTEM_NAME
there is the notice:
CMAKE_SYSTEM_NAME may be set explicitly when first configuring a new build tree in order to enable cross compiling. In this case the CMAKE_SYSTEM_VERSION variable must also be set explicitly
.
So when I specify -DCMAKE_SYSTEM_VERSION=9.3
my app is running without issues.
Comparing compilation logs I probably found the significant difference. When CMAKE_SYSTEM_VERSION
is missing then in linking phase there is option
-install_name /path_to_my_working_dir/cmake_system_version/build/Debug-iphonesimulator/framework_test.framework/framework_test
. When CMAKE_SYSTEM_VERSION
is specified this option is -install_name @rpath/framework_test.framework/framework_test
So either documentation must be changed to mention that -DCMAKE_SYSTEM_VERSION=
must be specified or CMake should take CMAKE_OSX_DEPLOYMENT_TARGET
into account when CMAKE_SYSTEM_VERSION
is not specified (probably initialise CMAKE_SYSTEM_VERSION
by CMAKE_OSX_DEPLOYMENT_TARGET
?).
I attached example application which demonstrates problem. There is app to open in Xcode: ios_test_app/test_app/test_app.xcodeproj
. When you 'Run' it will execute script build_ios.sh
which will configure and build framework. To check that -DCMAKE_SYSTEM_VERSION
really helps open the script build_ios.sh
and uncomment variable on top of file:
# If following variable is uncommented then iOS app successfully
# starts without runtime error.
# CMAKE_EXTRA_PARAMETERS="-DCMAKE_SYSTEM_VERSION=9.3"