c++ - 无法在 cmake 中包含外部先前构建的 .a 库

标签 c++ cmake

我有一个以前编译过的 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/

相关文章:

c++ - 如何将 libs 和 dlls 添加到 cmake 中的项目中?

c++ - 可以使用 Meson 构建一个简单的项目,但不能使用 CMake

c++ - 在 cmake 中添加库列表

c++ - std::list 删除中的意外结果 (C++)

c++ - MEX 无法找到 VS Express 2013 编译器 - MATLAB R2013a

带有桌面应用程序的 C++ 微服务

c++ - VS2010中如何设置相对路径

android - CMAKE_BUILD_TYPE 版本 : strange results

c++ - 适用于 Windows 上 Clang 的 CMake 生成器

c++ - C++中的随机函数