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
):
# 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"
. mpfit
is a function in libB
. Here is the strange thing now:
LibA
shared (by replacing STATIC
with SHARED
in CMake) I get no linker error! Someone told me that I will have to link all the shared libraries to App
myself. I tried that and it actually worked. But here is my question now:
PUBLIC
keyword in the target_link_libraries
function? LibA
is a shared library? EDIT1:
If that helps: If I do nm libA
I get:
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
. Which means that by the time you link to App
, some symbols of libB
are missing.
In that case you may want to specify linkers options to keep the whole libB
during the first linkage. For instance, for gcc you have --whole-archive ld parameter
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.