因此,我正在使用CMake(在Mac OS上)构建C ++项目,而我的项目依赖于dylib(我正在使用TBB https://www.threadingbuildingblocks.org/,但是特定的库本身并不重要)
如果我执行标准的“ cmake”和“ make”操作,则会在需要的位置生成可执行文件,并且在运行我的应用程序时,dylib可以正确链接,并且一切运行正常。
当我尝试执行“进行安装”并尝试从安装目录运行生成的可执行文件时,就会出现问题。我收到“找不到图像”错误:
dyld: Library not loaded: @rpath/libtbb.dylib
Referenced from:
/Users/MyName/Desktop/ProjectRoot/install/./MyApp
Reason: image not found
有趣的是,如果我执行常规的“ make”而不进行安装,然后将可执行文件手动复制到安装目录,则该将正确链接到我的dylib。我不知道为什么会这样。
我的目录结构如下:
Root
CMakeLists.txt
Source/
Libraries/
tbb/
include/
lib/
libtbb.dylib
install/
...并且我的CMakeLists.txt文件如下:
# Start of CMakeLists.txt
cmake_minimum_required(VERSION 3.3)
project (MyApp)
# Set C++ version and output paths
set (CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
set(CMAKE_MACOSX_RPATH 1)
set(CMAKE_INSTALL_PREFIX "${CMAKE_SOURCE_DIR}/install")
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
# Find TBB
find_library (
TBB_LIBRARIES
NAMES tbb libtbb # what to look for
HINTS "${CMAKE_SOURCE_DIR}/Libraries/tbb/lib" # where to look
NO_DEFAULT_PATH # do not search system default paths
)
# Set Custom Include Files + TBB header files
include_directories(Source/Headers Libraries/tbb/include)
# Set Source Files
file(GLOB_RECURSE SRC_FILES "Source/*.cpp")
add_executable(MyApp ${SRC_FILES})
# Link Libraries
target_link_libraries(MyApp ${TBB_LIBRARIES})
# Set compile flags
set_target_properties(MyApp PROPERTIES CXX_STANDARD 14) #LINK_FLAGS "-Wl")
target_compile_features(MyApp PUBLIC cxx_std_14)
# Install executable
install(TARGETS MyApp DESTINATION .)
如果我还尝试添加以下行,并同时安装dylib:
install(TARGETS ${TBB_LIBRARIES} DESTINATION lib)
然后,当我执行“ make install”时,出现以下错误:
install TARGETS given target
"/Users/MyName/Desktop/ProjectRoot/Libraries/tbb/lib/libtbb.dylib"
which does not exist in this directory.
所以我似乎无法使该安装正常工作。我该如何修复它,以使我的可执行文件和库都安装在正确的位置,并且我的可执行文件在运行时能够链接到我的库?