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:
- If I make
LibAshared (by replacingSTATICwithSHAREDin 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:
- Isn't CMake supposed to automatically propagate all the link dependencies to my executable? Isn't this the purpose of the
PUBLICkeyword in thetarget_link_librariesfunction? - Why I get no linker errors when
LibAis a shared library?
EDIT1:
If that helps: If I do nm libA I get:
libA.cpp.o:
// ... blah blah ...
U mpfit
// ... blah blah ...