[CMake] Difference between PRIVATE and PUBLIC with target_link_libraries

Attila Krasznahorkay attila.krasznahorkay at gmail.com
Tue May 10 05:39:49 EDT 2016


Hi Patrick,

I *think* that these public/private rules behave a bit differently for static libraries than they do for shared ones.

But I have to admit, that based on this code I also would've guessed that -I/tmp would not show up in the build of exe1...

I did manage to use public and private dependencies as expected in my own configurations, so I'm not exactly sure what's going wrong in your case. Which version of CMake did you use for the test?

Cheers,
              Attila

> On 10 May 2016, at 07:59, Patrick Boettcher <patrick.boettcher at posteo.de> wrote:
> 
> Hi list,
> 
> What is the differences between PRIVATE and PUBLIC when used with
> target_link_libraries?
> 
> I read the help and understood that it works like in C++: PRIVATE will
> make everything which was PUBLIC before also PRIVATE if inherited
> privately.
> 
> An example:
> 
>  add_library(lib1 INTERFACE)
>  target_include_directories(lib1 INTERFACE /tmp)
> 
>  add_library(lib2 src2.c)
>  target_include_directories(lib2 PUBLIC /bin)
>  target_link_libraries(lib2 PRIVATE lib1) # PRIVATE here
> 
>  add_library(lib3 src3.c)
>  target_include_directories(lib3 PUBLIC /bin)
>  target_link_libraries(lib3 PUBLIC lib1) # PUBLIC here
> 
>  add_executable(exe1 exe.c)
>  target_link_libraries(exe1 lib2)
> 
>  add_executable(exe2 exe.c)
>  target_link_libraries(exe2 lib3)
> 
> 
> When building exe2 both include-dirs (from lib1 and lib3) are present:
> 
>  [..] -I/bin -I/tmp  [..]
> 
> as they are for exe1 - however I would have expected to not see /tmp
> because lib3 and lib1 are linked privately.
> 
> Where is my mistake?
> 
> Thanks,
> --
> Patrick.
> 
> -- 
> 
> Powered by www.kitware.com
> 
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
> 
> Kitware offers various services to support the CMake community. For more information on each offering, please visit:
> 
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
> 
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
> 
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake



More information about the CMake mailing list