[英]How do I link the library correctly using cmake?
I have laid out my project directory below,so when I do cmake build I get a linker error which I wasn't able to figure out.我已经在下面列出了我的项目目录,所以当我做 cmake build 时,我得到了一个我无法弄清楚的链接器错误。 I get a linker error that LIBD cannot be found, although LIBB is successfully formed and APP executable only needs LIBB why is it throwing a linker error that it needs LIBD when trying to build APP?
我收到一个链接器错误,即 LIBD 无法找到,尽管 LIBB 已成功形成并且 APP 可执行文件只需要 LIBB 为什么它在尝试构建 APP 时抛出它需要 LIBD 的链接器错误?
|---CMakeLists.txt <==== add_subdirectory(source) , add_subdirectory(apps)
|---build
|---include
|---a.h
|---b.h
|---apps|
|---CMakeLists.txt
|---apps.cpp 1] target_link_libraries(APP PUBLIC LIBB) <==== linker error
LIBD not found
|---source
| |---CMakeLists.txt ===> 1]link_directories(PATH TO LIBD)
2]target_link_libraries(LIBA public LIBC) <== successful
3]target_link_libraries (LIBB public LIBA LIBD) <== successful
|---a.cpp
|---b.cpp
|---lib
| |---LIBD <===== static library
link_directories
is scoped to the file containing it, in your case source/CMakeLists.txt
, so apps/CMakeLists.txt
does not know where to find LIBD. link_directories
的范围限定为包含它的文件,在您的情况下为source/CMakeLists.txt
,因此apps/CMakeLists.txt
不知道在哪里可以找到 LIBD。
You should create an IMPORTED CMake target in the main CMakeLists.txt instead and link to that where you need it:您应该在主 CMakeLists.txt 中创建一个 IMPORTED CMake 目标,并链接到您需要的位置:
add_library(LIBD IMPORTED)
set_target_properties(LIBD PROPERTIES
IMPORTED_LOCATION lib/LIBD.a)
Or, alternatively, you could repeat the link_directories
statement in your apps/CMakeLists.txt
.或者,您可以在
apps/CMakeLists.txt
重复link_directories
语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.