如何让 CMake 将可执行文件链接到不在同一 CMake 项目中构建的外部共享库?
只是在做target_link_libraries(GLBall ${CMAKE_BINARY_DIR}/res/mylib.so)
给出错误
make[2]: *** No rule to make target `res/mylib.so', needed by `GLBall'. Stop.
make[1]: *** [CMakeFiles/GLBall.dir/all] Error 2
make: *** [all] Error 2
(GLBall is the executable)
将库复制到二进制目录后bin/res
.
我尝试使用find_library(RESULT mylib.so PATHS ${CMAKE_BINARY_DIR}/res)
哪个失败了RESULT-NOTFOUND
.
arrowdodger 的答案是正确的,并且在很多情况下都是首选。我只想为他的答案添加一个替代方案:
您可以添加“导入的”库目标,而不是链接目录。就像是:
# Your-external "mylib", add GLOBAL if the imported library is located in directories above the current.
add_library( mylib SHARED IMPORTED )
# You can define two import-locations: one for debug and one for release.
set_target_properties( mylib PROPERTIES IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/res/mylib.so )
然后链接,就像这个库是由您的项目构建的一样:
TARGET_LINK_LIBRARIES(GLBall mylib)
这种方法会给你更多的灵活性:看看添加库(导入) https://cmake.org/cmake/help/latest/command/add_library.html#imported-libraries命令和许多与导入库相关的目标属性 https://cmake.org/cmake/help/latest/manual/cmake-properties.7.html#properties-on-targets.
我不知道这是否可以解决您的“更新版本的库”问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)