Cmake:制作静态库并在其他项目中使用

Cmake: make static library and use in other projects

本文关键字:项目 其他 静态 Cmake      更新时间:2023-10-16

我需要制作库test并在其他项目中使用它#include <libraryname>

在项目 test 中:

CmakeLists.txt

...add_library(libtest STATIC lib.h lib.cpp(set_target_properties(libtest PROPERTIES PUBLIC_HEADER lib.h(install(TARGET libtest        PUBLIC_HEADER目的地包括        运行时目标箱        存档目标库        库目标库(...

在项目 testUseLibrary 中:

CmakeLists.txt

...add_executable(项目主.cpp(target_link_libraries(项目公共自由测试(...

主.cpp

#include <libtest> // error 
int main() {
    return 0;
}

在项目中testUseLibrary #include<libtest>看不到库。

您需要

使用 target_include_directories 来指定目标的包含目录。在本例中为 libtest 所在的目录。