简体   繁体   English

CMake没有将共享库的链接依赖性传播到我的可执行文件

[英]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: 现在这是奇怪的事情:

  • If I make 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: 但现在我的问题是:

  • Isn't CMake supposed to automatically propagate all the link dependencies to my executable? 是不是CMake应该自动将所有链接依赖项传播到我的可执行文件? Isn't this the purpose of the PUBLIC keyword in the target_link_libraries function? 这不是target_link_libraries函数中PUBLIC关键字的用途吗?
  • Why I get no linker errors when 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.

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