I'm working on a C++ project that uses cmake and we're having issues with inflated build times for some targets. After some digging, I found unnecessary libraries are listed in some of the target_link_libraries
.
In the following example, main depends on A, which depends on B, so when main is compiled, A and B also get built.
add_library(A STATIC a.c)
add_library(B STATIC b.c)
target_link_libraries(A B)
add_executable(main main.c)
target_link_libraries(main A)
However, A doesn't include any files from B, so it doesn't need to be listed in the target_link_libraries
. Is there a way to detect that B is unnecessary?
ldd <executable/library>
. Then you may compare (either manually, or create some script) list of useful links with a list of all links. According to this comparison you may remove unneededtarget_link_libraries
from yourCMakeLists.txt
.