我有一个包含源文件的C++项目。对于外部项目,我需要一些文件夹来搜索包含的库:
/home/data/lib/wisenet
/home/data/lib/wise_log
/home/data/lib/wise_rs_device
/home/data/lib/json
/home/data/lib/wise_versioning
我必须写些什么才能在CMake中包含这些外部库?这些文件夹仅包含界面资源(h文件和.a库)。
我试图包括这些目录:
include_directories(
/home/data/lib/wisenet
/home/data/lib/wise_log
... etc
)
而且我不明白如何正确添加lib文件,例如
libwise_rs_device.a
。
最佳答案
包括目录仅用于...好吧,包括代码的路径。它不会链接库。
使用外部库的正确方法是使用导入的库:
add_library(wise_rs_device STATIC IMPORTED GLOBAL)
set_target_properties(wise_rs_device PROPERTIES
IMPORTED_LOCATION "path/to/static/library"
INTERFACE_INCLUDE_DIRECTORIES "path/to/headers/of/wise_rs_device"
)
然后,您只需将导入的目标链接到您的目标:
# will link to the static library and add include directories.
target_link_libraries(your_executable PRIVATE wise_rs_device)
关于c++ - CMake无法链接外部库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59951141/