[CMake] Difference between PRIVATE and PUBLIC with target_link_libraries

Patrick Boettcher patrick.boettcher at posteo.de
Tue May 10 01:59:48 EDT 2016


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.



More information about the CMake mailing list