[iOS CMake] Library not loaded: @rpath/libSDL2-2.0d.dylib (no such file)

Hi. I’ve got an issue upon deployment of simple SDL-based app on real iOS device.
I’m using cmake to generate xcode project. In XCode project I’m building and running app, but I’m getting crash right after its launch.
Here is how my cmake file looks like:

cmake_minimum_required(VERSION 3.10) 
 
set(PROJECT_NAME SDL_RND) 
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) 
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_CXX_FLAGS "-std=c++14 ${CMAKE_CXX_FLAGS}")

set(MACOSX_BUNDLE_GUI_IDENTIFIER "myapp.sdl.rnd")
set(MACOSX_BUNDLE_BUNDLE_VERSION "0.0.1")
set(MACOSX_BUNDLE_SHORT_VERSION_STRING "0.0.1")

macro (set_xcode_property TARGET XCODE_PROPERTY XCODE_VALUE)
    set_property (TARGET ${TARGET} PROPERTY XCODE_ATTRIBUTE_${XCODE_PROPERTY} ${XCODE_VALUE})
endmacro (set_xcode_property)
 
project(${PROJECT_NAME} VERSION 0.0.1) 
 
file(GLOB_RECURSE SOURCES "${PROJECT_SOURCE_DIR}/package/src/*.cpp") 
 
add_subdirectory(extern/SDL) 
 
add_executable(${PROJECT_NAME} ${SOURCES}) 
 
set_target_properties( 
    ${PROJECT_NAME} PROPERTIES 
    CXX_STANDART 14 
    CXX_STANDART_REQUIRED ON 
) 
 
target_include_directories(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/package/include/) 
target_include_directories(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/extern/) 
 
target_link_libraries(${PROJECT_NAME} SDL2 SDL2main)

set_xcode_property(${PROJECT_NAME} CODE_SIGN_IDENTITY "*****")
set_xcode_property(${PROJECT_NAME} DEVELOPMENT_TEAM "*****")
set_xcode_property(SDL2 CODE_SIGN_IDENTITY "*****")
set_xcode_property(SDL2 DEVELOPMENT_TEAM "*****")

~/extern folder contains only sdl folder with 2.0.18 sdl contents, ~/package folder contains only my cpp and hpp files.

Generation command:

cmake -G Xcode .. -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphoneos -DCMAKE_OSX_ARCHITECTURES="arm64"

After this I build for connected iphone 10 (iOS 15.2.1) with SDL_RND specified as a target. After resolving of untrusted author issue I’m trying to run app but gettings this crash:

ReportCrash	ASI found [dyld] (sensitive) 'Library not loaded: @rpath/libSDL2-2.0d.dylib\
  Referenced from: /private/var/containers/Bundle/Application/3CDEA049-C9C0-4427-A3E3-334827725541/SDL_RND.app/SDL_RND\
  Reason: tried: '/usr/lib/system/introspection/libSDL2-2.0d.dylib' (no such file), '/Users/*****/Desktop/CMakeSDL/build/Debug/libSDL2-2.0d.dylib' (no such file), '/Users/*****/Desktop/CMakeSDL/build/Debug/libSDL2-2.0d.dylib' (no such file), '/usr/local/lib/libSDL2-2.0d.dylib' (no such file), '/usr/lib/libSDL2-2.0d.dylib' (no such file)'\

Actually I have libSDL2-2.0d.dylib next to SDL_RND.app in Debug folder, but seems like it was not copied to my device. Strange, but I’m not having same issue on iphonesimulator and everything works great there. Log with the part of logcat with crash info:
ios_crash_logcat.rtf (132.7 KB)

macOS Monterey 12.1, XCode 13.2.1

Will be very appreciated for any advices. Thanks.

It needs to be included in your app’s bundle.

It’s probably gonna be easier if you build with either the static library or the SDL2 framework.

Thanks for your reply. I’m looking for a generate-build-run solution for iOS similar to iphonesimulator workflow. Is there a way to do it? Maybe I should adjust my CMakeLists in some way? I’m quite new to this

Yeah, you’ll have to adjust your CMakeLists

I don’t remember exactly what you need to specify. You have to tell CMake that you want to link to the framework, then tell it where to look.

Dunno what you need to add to CMakeLists.txt so the generated Xcode project file will have SDL2 framework bundled in the app bundle, but IIRC there’s a command for it. I know that if you manually add a non-system framework to your app in Xcode then it automatically sets everything up to copy it to your app bundle.

For generate-build-run, to build from the command line there’s the xcodebuild tool. No idea how to launch your app on the device from the command line.

Yeap, it finally worked after I replaced SDL2 with SDL2-static in CMakeLists, thanks!
Still strange it wasn’t worked with dynamic lib by default even after I added libSDL2-2.0d.dylib into xcode’s frameworks

The dylib wasn’t being copied to the device because it wasn’t in the app bundle.

1 Like