[CMake] Transitive linking

Alexander Neundorf a.neundorf-work at gmx.net
Fri Nov 18 15:40:15 EST 2011


On Friday 18 November 2011, James Bigler wrote:
> I thought CMake knew how to not drag all the dependent libraries once you
> linked an executable module.
> 
> add_library(A STATIC a.cpp)
> add_library(B SHARED b.cpp)
> target_link_libraries(B A)
> add_library(C SHARED c.cpp)
> target_link_libraries(C B)
> 
> add_executable(run run.cpp)
> target_link_libraries(run C)
> 
> At this point I'm seeing that C links against B and A when I think it
> should only link against B since A shouldn't be needed to link against B.
> In addition when compiling run, it links against B and A.
> 
> /usr/bin/c++   -dynamiclib -Wl,-headerpad_max_install_names   -o libC.dylib
> -install_name /Users/jbigler/tmp/code/cmake/translinking/build/libC.dylib
> CMakeFiles/C.dir/c.cpp.o libB.dylib libA.a
> /usr/bin/c++    -Wl,-search_paths_first -Wl,-headerpad_max_install_names
> CMakeFiles/run.dir/run.cpp.o  -o run  libC.dylib libB.dylib libA.a
> 
> Is this the expected behavior?  

Yes.
If you want to limit this, use target_link_libraries( C 
LINK_INTERFACE_LIBRARIES ... ), with this you can specify the transitively 
linked libraries when linking against C.

Alex


More information about the CMake mailing list