[CMake] Transitive behavior of target_link_libraries between shared/static

Eric Noulard eric.noulard at gmail.com
Tue Jul 18 12:02:40 EDT 2017


2017-07-17 17:31 GMT+02:00 Robert Dailey <rcdailey.lists at gmail.com>:

> Suppose I have the following:
>
> ```
> add_library( A STATIC ${files} )
>
> add_library( B SHARED ${more_files} )
> target_link_libraries( B PUBLIC A )
>
> add_library( C STATIC ${even_more_files} )
> target_link_libraries( C PUBLIC B )
>
>
> add_executable( D ${exe_files} )
> target_link_libraries( D PRIVATE C )
> ```
>
> Does the linker command for target `D` ever see target A's static link
> library? I'm hoping the only thing the linker gets is `libB.so` and
> `libC.a`, without `libA.a`.
>

I guess it does otherwise you may be missing symbols.
As explain by Peter some time ago:
  https://cmake.org/pipermail/cmake/2017-April/065347.html
when you
target_link_libraries( B PUBLIC A )

with A being STATIC you do not really link A into B (which is shared in
your example)
but you add A as a PUBLIC item of the link interface of B such that when
actual linking
occurs if you build an executable linked to B then A is automagically
brought to the link step.

More info on that topic by Craig:
https://cmake.org/pipermail/cmake/2016-May/063400.html


> It seems like A to everyone outside of B should just be a set of
> public include directories and compile definitions, since the link
> file aspect of it should be hidden across shared library boundaries.
>

No it is not, because the binary object inside A are NOT embedded into B.
If you want that then you need to make A an OBJECT library and not a STATIC
library.



> Hoping someone can explain how this works. I want to make sure static
> libs sitting behind shared libs in the dependency tree do not get
> exposed.
>

My understanding is that you cannot do what you want with static lib.
You can do that with OBJECT lib though.
Knowing that you cannot use target_link_libraries with OBJECT lib:
https://cmake.org/cmake/help/v3.9/manual/cmake-buildsystem.7.html#object-libraries

Hope this helps.

-- 
Eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20170718/0ec9df0a/attachment.html>


More information about the CMake mailing list