3
votes

Im trying to build an OSX bundle with Clion and Cmake, Currently the library links correctly in terms of I have no pre compile errors but when I build and try to run the program I get the following error


    dyld: Library not loaded: libxl.dylib
    Referenced from: 
    /path/to/executable/
      Reason: image not found

Ive searched on this issue and cant seem to find a clear solution, This is probably due to my lack of experience with CMake and C++.

If anyone has a solution or can point me in the direction of what I should be reading to further understand this it would be much appreciated.

See CMakeLists.txt below

    cmake_minimum_required(VERSION 3.7)
    project(project name)

    set(CMAKE_CXX_STANDARD 14)

    set(SOURCE_FILES main.cpp)

    find_package( Qt5Core REQUIRED )
    find_package( Qt5Widgets REQUIRED )
    find_package( Qt5Gui REQUIRED )

    set(PROJECT_LINK_LIBS libxl.dylib)
    link_directories(${CMAKE_SOURCE_DIR}/LibXL/lib)
    include_directories(${CMAKE_SOURCE_DIR}/LibXL/include_cpp)


    #Mac Bundle (Built on Mac)
    add_executable(project_target_mac MACOSX_BUNDLE main.cpp)
    qt5_use_modules( project_target_mac Core Widgets Gui )
    target_link_libraries(project_target_mac Qt5::Widgets)
    target_link_libraries(project_target_mac ${PROJECT_LINK_LIBS} )
    #set_target_properties(project_target_mac PROPERTIES INSTALL_RPATH "${CMAKE_SOURCE_DIR}/LibXL/lib")

    add_executable(project_target ${SOURCE_FILES})
    qt5_use_modules( project_target Core Widgets Gui )
    target_link_libraries(project_target Qt5::Widgets)
    target_link_libraries(project_target ${PROJECT_LINK_LIBS

} )
1
I recently patched Tagaini Jisho project for this, may be you can use my pull request as example: github.com/Gnurou/tagainijisho/pull/195user2266462
Thanks I will take a lookjustin018

1 Answers

0
votes

On OSX usually man uses Frameworks and integrate it into app bundle with an install rule.

The rule looks like:

    install( DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/ofxSyphon/libs/Syphon/lib/osx/Syphon.framework"
    DESTINATION ${APP_NAME}.app/Contents/Frameworks")

Then the framework folder will be copied into the Contents/Frameworks subfolder of the application bundle. For this to work, the application itself should be installed with something like:

install(TARGETS ${APP_NAME} DESTINATION .)

And you might want to specify the installation prefix with CMAKE_INSTALL_PREFIX, default is /usr/local.

For a single library file, the process is quite similar, but you also have to tweak the RPATH of the application to add the relative path of the lib for the executable to find it. This part is not needed for framework because they have a standardized folder structure and the executable looks into it for its dependencies.