[CMake] Transitive linking

James Bigler jamesbigler at gmail.com
Fri Nov 18 16:03:22 EST 2011


2011/11/18 Alexander Neundorf <a.neundorf-work at gmx.net>

> 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
>

OK, so propagating the libraries is the default behavior.  I tried to use
LINK_INTERFACE_LIBRARIES, but it only seemed to work for static libraries:

add_library(A STATIC a.cpp)
add_library(B SHARED b.cpp)
target_link_libraries(B A)
target_link_libraries(B LINK_INTERFACE_LIBRARIES)
add_library(C SHARED c.cpp)
target_link_libraries(C B)
target_link_libraries(C LINK_INTERFACE_LIBRARIES)

add_executable(run run.cpp)
target_link_libraries(run C)

During build:

Build B (this is fine)
/usr/bin/c++   -dynamiclib -o libB.dylib CMakeFiles/B.dir/b.cpp.o libA.a
Build C (this is fine too, no A in the list)
/usr/bin/c++   -dynamiclib -o libC.dylib CMakeFiles/C.dir/c.cpp.o libB.dylib
Build run (this is weird, it linked both B and C)
/usr/bin/c++    CMakeFiles/run.dir/run.cpp.o  -o run  libC.dylib libB.dylib

Did I specify something wrong or does this property only consider static
libraries?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20111118/2b8b0566/attachment-0001.htm>


More information about the CMake mailing list