简体   繁体   English

如何将我的静态库链接到我的C ++项目CMake?

[英]How to link my static library to my c++ project CMake?

I'm using CLion with CMake. 我在CMake中使用CLion。 I have my own static library "libxxx.a". 我有自己的静态库“ libxxx.a”。 I'm trying to link it this way in CMakeLists.txt: target_link_libraries(myProject ./lib/libxxx.a) And this way I'm including library to my main.cpp. 我试图以这种方式在CMakeLists.txt target_link_libraries(myProject ./lib/libxxx.a)其链接: target_link_libraries(myProject ./lib/libxxx.a)并且这样,我将库包含到了main.cpp中。 #include "xxx.h" . #include "xxx.h" But I have error fatal error: xxx.h: No such file or directory . 但是我有fatal error: xxx.h: No such file or directory What should I do? 我该怎么办?

CMake include a prebuilt static library CMake包含一个预建的静态库

As a sketch you need your project to look something like this 作为草图,您需要您的项目看起来像这样

project( myProject )

set( SOURCE_FILES main.cpp )

add_library( myLibrary STATIC IMPORTED )
set_property( TARGET myLibrary PROPERTY IMPORTED_LOCATION /path/to/lib/libxxx.a )
include_directories( /path/to/headers/ )

add_executable( myProject ${SOURCE_FILES} )
target_link_libraries( myProject myLibrary )

You might be able to replace include_directories with: 您可能可以将include_directories替换为:

set_property( TARGET myLibrary PROPERTY INCLUDE_DIRECTORIES /path/to/headers/ )

A better way to go is to compile the static library from source and then use 更好的方法是从源代码编译静态库,然后使用

target_include_directories( myLibrary PUBLIC /path/to/headers/ )

And then they get handled automatically. 然后它们会自动处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM