[英]CMake is not propagating link dependencies of shared libraries to my executable
I have asked this question in the past but in a different context. 我过去曾问过这个问题但是在不同的背景下。 I have a C++ application (
App
) which links to a static C++ library A ( libA
), which links to a static C library B ( libB
): 我有一个C ++应用程序(
App
)链接到静态C ++库A( libA
),它链接到静态C库B( libB
):
# App CMakeLists.txt
add_executable(App ${APP_HEADER_FILES} ${APP_SOURCE_FILES})
target_link_libraries(App PUBLIC LibA)
# LibA CMakeLists.txt
add_library(LibA STATIC ${LIBA_HEADER_FILES} ${LIBA_SOURCE_FILES})
target_link_libraries(LibA PUBLIC LibB)
The problem is that when I build App
I get a linker error: "error: undefined reference to mpfit"
. 问题是,当我构建
App
我得到一个链接器错误: "error: undefined reference to mpfit"
。 mpfit
is a function in libB
. mpfit
是一个功能libB
。 Here is the strange thing now: 现在这是奇怪的事情:
LibA
shared (by replacing STATIC
with SHARED
in CMake) I get no linker error! LibA
共享(通过在CMake中用SHARED
替换STATIC
)我没有链接器错误! Someone told me that I will have to link all the shared libraries to App
myself. 有人告诉我,我必须自己将所有共享库链接到
App
。 I tried that and it actually worked. 我试过了,它确实有效。 But here is my question now:
但现在我的问题是:
PUBLIC
keyword in the target_link_libraries
function? target_link_libraries
函数中PUBLIC
关键字的用途吗? LibA
is a shared library? LibA
是共享库时,为什么我没有链接器错误? EDIT1: EDIT1:
If that helps: If I do nm libA
I get: 如果这有帮助:如果我做
nm libA
我得到:
libA.cpp.o:
// ... blah blah ...
U mpfit
// ... blah blah ...
It's quite possible that the linker is dropping non referenced symbols when linking libB
to libA
. 当将
libB
链接到libA
时,链接器很可能丢弃未引用的符号。 Which means that by the time you link to App
, some symbols of libB
are missing. 这意味着当您链接到
App
,会丢失一些 libB
符号。
In that case you may want to specify linkers options to keep the whole libB
during the first linkage. 在这种情况下,您可能希望指定链接器选项以在第一次链接期间保留整个
libB
。 For instance, for gcc you have --whole-archive ld parameter 例如,对于gcc,你有--whole-archive ld参数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.