gpt4 book ai didi

c++ - 如何在 C++ 项目的 Cmake 文件中添加 Mac OS 框架

转载 作者:行者123 更新时间:2023-11-30 04:42:28 26 4
gpt4 key购买 nike

我正在尝试将外部库 (Vulkan) 添加到我的项目中。这个库是预编译的,有一个框架。

我的项目树:

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

在我的 CMakeLists.txt 中,我尝试像这样添加我的 vulkan 库:

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()

然后

TARGET_LINK_LIBRARIES(my_exe  ${LIB_VULKAN})

当我在我的构建目录中使用 cmake .. 然后使用 make 时,它工作得很好,但是当我运行我的可执行文件时 ./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

我想我也必须在我的 CMake 中设置框架,但我没有找到使 my_exe 工作的解决方案。

编辑:这是我的可执行文件的 otool -L 的输出

./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)

这是 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

最佳答案

您正在链接 ../../../../ext/vulkan/macos/lib/libshaderc_shared.dylib 并且如果您运行 otool -L 在上面你肯定会看到它认为它被称为 /tmp/.../libshaderc_shared.dylib 这是你问题的根本原因(它基本上告诉运行时链接器在哪里获取它库来自)。

该文件已损坏,您只能通过在其上运行 install_name_tool 将其重命名为使用 @rpath 来修复它,就像其他库一样。

但您不需要链接到所有这些库,只需将 CMakeLists.txt 文件更改为仅链接到 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()

关于c++ - 如何在 C++ 项目的 Cmake 文件中添加 Mac OS 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58728757/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com