【问题标题】:Cmake: make static library and use in other projectsCmake:制作静态库并在其他项目中使用
【发布时间】:2019-08-31 05:29:25
【问题描述】:

我需要创建库test 并通过#include <libraryname> 在其他项目中使用它

在项目test:

CmakeLists.txt

... add_library(libtest 静态 lib.h lib.cpp) set_target_properties(libtest PROPERTIES PUBLIC_HEADER lib.h) 安装(目标 libtest PUBLIC_HEADER DESTINATION 包括 运行时目标箱 存档目标库 图书馆目的地库) ...

在项目testUseLibrary

CmakeLists.txt

... add_executable(项目 main.cpp) target_link_libraries(项目 PUBLIC libtest) ...

main.cpp

#include <libtest> // error 

int main() {
    return 0;
}

在项目中 testUseLibrary #include&lt;libtest&gt; 没有看到库。

【问题讨论】:

    标签: c++ cmake static-libraries


    【解决方案1】:

    您需要使用target_include_directories 为您的目标指定包含目录。在这种情况下,libtest 所在的目录。

    【讨论】:

    • 也许存在使用 find_library 的方法?
    • 您的libtest 项目应该创建一个package configuration,而消费者应该创建一个find_package(libtest REQUIRED)。导入的libtest 目标将在那里可用。
    猜你喜欢
    • 1970-01-01
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    • 2013-04-30
    • 2019-11-13
    • 1970-01-01
    相关资源
    最近更新 更多