Runtime dylib search path wrong

Short version
Why is RPATH disabled in CMake by default?

Background
I want to include a dylib file in my plugin and it is present as a bundle resource.
However, on runtime the app is looking into the hardcoded build directory, so if I move the app to another computer, it will complain about missing .dylib.

I found two ways to do this:
1.
CMAKE_XCODE_ATTRIBUTE_LD_RUNPATH_SEARCH_PATHS "@loader_path/../Resources"

But this is an XCode specific flag and it does not feel like a clean solution.
2.
set_property(TARGET ${PROJECT_NAME}_${format} PROPERTY BUILD_RPATH "@loader_path/../Resources" )

AND

set(CMAKE_SKIP_RPATH "NO" CACHE INTERNAL "")

The end goal boils down to these results of running otool -l:

Working
Load command 39
cmd LC_RPATH
cmdsize 40
path @loader_path/…/Resources (offset 12)

Broken
Load command 39
cmd LC_RPATH
cmdsize 64
path /Users/xxx/Dev/git/xxxx/Libraries (offset 12)

So why do I have to force RPATH to be used?
Am I missing something? Is there a cleaner way of doing this?

what I do is usually something like that


    add_custom_command(
        TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND
        	${CMAKE_COMMAND} -E copy
            "/usr/local/lib/myLib.dylib"
            "$<TARGET_FILE_DIR:${PROJECT_NAME}>/../Frameworks/myLib.dylib"
    )
    add_custom_command(
        TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND install_name_tool -change @rpath/myLib.dylib @loader_path/../Frameworks/myLib.dylib $<TARGET_FILE:${PROJECT_NAME}>
   )
1 Like