Issue
If we create libFoo.a
first with add_library(Foo STATIC foo.cpp foo.h)
and then do
add_library(Foo STATIC IMPORTED)
set_target_properties(Foo PROPERTIES IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/libFoo.a)
add_library(Bar STATIC bar.cpp bar.h)
target_link_libraries(Bar Foo)
After touch libFoo.a
and then make
there's no rebuild. Why is that?
What happened in my real situation is that after I update one of my third party dependencies by compiling their source and running make install
, the build folder in my project that finds the installed third party dependencies via find_package
does not get rebuild. I had to delete the build folder and compile from scratch to get the proper updates. Is this a bug in CMake?
Solution
While you write
target_link_libraries(Bar Foo)
there is no dependency between libBar.a
and libFoo.a
, so modifying the latter doesn't trigger rebuilding the former.
This is because Bar
is a STATIC library, and creation of a static library doesn't involve linking step.
However, modifying libFoo.a
would trigger rebuilding a shared library (or executable) which is linked with Bar
via target_link_libraries
:
add_library(Baz SHARED baz.c)
# That linkage makes libBaz.so to be dependent from libFoo.a
target_link_libraries(Baz Bar)
Despite that Baz
is explicitly linked only with Bar
, CMake automatically adds libFoo.a
to be linked libraries of Baz
and creates appropriate dependency.
Answered By - Tsyvarev Answer Checked By - Senaida (WPSolving Volunteer)