how to add Mac OS Framework on Cmake file for C++ project

六眼飞鱼酱① 提交于 2020-01-15 10:27:10

问题


I am trying to add an external library (Vulkan) to my project. This library is pre-compiled and has a framework.

My project tree:

- build
- source
  - Entry
     - main.cpp
- include
- ext
  - vulkan
      - macos
         - include
         - lib
         - Frameworks
- CMakeLists.txt

In my CMakeLists.txt, I try to add the lib of my vulkan lib like this:

if(APPLE)
  set(vulkan_lib_dir ${CMAKE_SOURCE_DIR}/ext/vulkan/macos/lib)
    file(GLOB LIB_VULKAN ${vulkan_lib_dir}/*.dylib )
  INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/ext/vulkan/macos/include)
endif()

and then

TARGET_LINK_LIBRARIES(my_exe  ${LIB_VULKAN})

When I use cmake .. and then make on my build directory, it works perfectly but when I run my executable ./my_exe

dyld: Library not loaded: /tmp/macos-sdk-build/shaderc/src/build/libshaderc/libshaderc_shared.1.dylib
  Referenced from: /Users/benz/Documents/Dev/build/my_exe/source/Entry/./my_exe
  Reason: image not found

I think I have to set the framework in my CMake too, but I did not find a solution that made my_exe work.

EDIT : this is the output of otool -L of my executable

./S2Engine:
    @rpath/libMoltenVK.dylib (compatibility version 1.0.0, current version 1.0.0)
    @rpath/libVkLayer_api_dump.dylib (compatibility version 0.0.0, current version 0.0.0)
    @rpath/libVkLayer_core_validation.dylib (compatibility version 0.0.0, current version 0.0.0)
    @rpath/libVkLayer_khronos_validation.dylib (compatibility version 0.0.0, current version 0.0.0)
    @rpath/libVkLayer_object_lifetimes.dylib (compatibility version 0.0.0, current version 0.0.0)
    @rpath/libVkLayer_stateless_validation.dylib (compatibility version 0.0.0, current version 0.0.0)
    @rpath/libVkLayer_thread_safety.dylib (compatibility version 0.0.0, current version 0.0.0)
    @rpath/libVkLayer_unique_objects.dylib (compatibility version 0.0.0, current version 0.0.0)
    /tmp/macos-sdk-build/shaderc/src/build/libshaderc/libshaderc_shared.1.dylib (compatibility version 1.0.0, current version 0.0.0)
    @rpath/libvulkan.1.dylib (compatibility version 1.0.0, current version 1.1.121)
    /System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 22.0.0)
    /System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
    /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1350.10.0)
    /System/Library/Frameworks/CoreVideo.framework/Versions/A/CoreVideo (compatibility version 1.2.0, current version 1.5.0)
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 307.5.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.60.2)
    /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (compatibility version 45.0.0, current version 1504.84.100)
    /System/Library/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics (compatibility version 64.0.0, current version 1070.22.1)
    /System/Library/Frameworks/CoreServices.framework/Versions/A/CoreServices (compatibility version 1.0.0, current version 775.20.0)
    /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1349.93.0)
    /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)

this is the output of otool -L of libvulkan.dylib

libvulkan.dylib:
    @rpath/libvulkan.1.dylib (compatibility version 1.0.0, current version 1.1.121)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.200.5)
    /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1560.12.0)
[100%] Linking CXX executable S2Engine
cd /Users/benz/Documents/S2EngineDEV/build/S2Engine/source/Entry && /usr/local/Cellar/cmake/3.11.3/bin/cmake -E cmake_link_script CMakeFiles/S2Engine.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  -g -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/S2Engine.dir/main.cpp.o CMakeFiles/S2Engine.dir/__/Manager/EnvironmentManager.cpp.o CMakeFiles/S2Engine.dir/__/Manager/LoggerManager.cpp.o CMakeFiles/S2Engine.dir/__/Manager/MemoryManager.cpp.o CMakeFiles/S2Engine.dir/__/Manager/WindowManager.cpp.o CMakeFiles/S2Engine.dir/__/Logger/Logger.cpp.o CMakeFiles/S2Engine.dir/__/Logger/FileLog.cpp.o CMakeFiles/S2Engine.dir/__/Memory/Memory.cpp.o CMakeFiles/S2Engine.dir/__/Memory/StackAllocator.cpp.o CMakeFiles/S2Engine.dir/__/Window/Window.cpp.o CMakeFiles/S2Engine.dir/__/Window/PatternWindow.cpp.o CMakeFiles/S2Engine.dir/__/Window/PatternGLFW3_VULKAN.cpp.o  -o S2Engine -Wl,-rpath,/Users/benz/Documents/S2EngineDEV/ext/vulkan/macos/lib ../../../ext/glfw33/src/libglfw3.a ../../../../ext/vulkan/macos/lib/libMoltenVK.dylib ../../../../ext/vulkan/macos/lib/libSPIRV-Tools-shared.dylib ../../../../ext/vulkan/macos/lib/libVkLayer_api_dump.dylib ../../../../ext/vulkan/macos/lib/libVkLayer_core_validation.dylib ../../../../ext/vulkan/macos/lib/libVkLayer_khronos_validation.dylib ../../../../ext/vulkan/macos/lib/libVkLayer_object_lifetimes.dylib ../../../../ext/vulkan/macos/lib/libVkLayer_stateless_validation.dylib ../../../../ext/vulkan/macos/lib/libVkLayer_thread_safety.dylib ../../../../ext/vulkan/macos/lib/libVkLayer_unique_objects.dylib ../../../../ext/vulkan/macos/lib/libshaderc_shared.1.dylib ../../../../ext/vulkan/macos/lib/libshaderc_shared.dylib ../../../../ext/vulkan/macos/lib/libvulkan.1.1.121.dylib ../../../../ext/vulkan/macos/lib/libvulkan.1.dylib ../../../../ext/vulkan/macos/lib/libvulkan.dylib -framework Cocoa -framework IOKit -framework CoreFoundation -framework CoreVideo 
[100%] Built target S2Engine
/usr/local/Cellar/cmake/3.11.3/bin/cmake -E cmake_progress_start /Users/benz/Documents/S2EngineDEV/build/CMakeFiles 0

回答1:


You are linking in ../../../../ext/vulkan/macos/lib/libshaderc_shared.dylib and if you ran otool -L on it you would no doubt see that it thinks it's called /tmp/.../libshaderc_shared.dylib which is the root-cause of your issue (it basically tells the runtime linker where to fetch that library from).

That file is broken and you can only fix it by running install_name_tool on it to rename it to use @rpath like the other libraries.

But you don't need to be linking against all those libraries so simply change the CMakeLists.txt file to only link against libvulkan.dylib:

if(APPLE)
  set(LIB_VULKAN ${CMAKE_SOURCE_DIR}/ext/vulkan/macos/lib/libvulkan.dylib)
  INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/ext/vulkan/macos/include)
endif()


来源:https://stackoverflow.com/questions/58728757/how-to-add-mac-os-framework-on-cmake-file-for-c-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!