我有一个以前编译过的 libgarithm.a 库和一个头文件 garith.h 我如何将它导入我的 cmake 项目。我已经包含了头文件 include_directories("/home/gaurav/Desktop/garith-lib/include") 但无法链接库,它给出了 comile 时间错误 --- 未定义对我库中的函数“multi(int, int)”的引用
最佳答案
你应该创建一个 imported target为您的图书馆然后使用target_link_libraries
:
add_library(garithm STATIC IMPORTED)
set_property(TARGET garithm PROPERTY IMPORTED_LOCATION
/path/to/libgarithm.a
)
set_property(TARGET garithm PROPERTY INTERFACE_INCLUDE_DIRECTORIES
/home/gaurav/Desktop/garith-lib/include
)
...
add_executable(foo main.cpp)
target_link_libraries(foo garithm)
包含目录也在导入的目标上声明,因此您不必调用 include_directories
编辑:target_include_directories
不适用于导入的目标,改为设置属性INTERFACE_INCLUDE_DIRECTORIES
关于c++ - 无法在 cmake 中包含外部先前构建的 .a 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38992760/