2

As explained here, in modern cmake you should keep include/link targets private. Your CMakeList.txt should somewhat look like this:

add_executable(myexe main.cpp)
target_include_directories(myexe PRIVATE ${mylib_include_dir})
target_link_libraries(myexe PRIVATE mylib)

Can it make a difference, regarding build time, if I use PRIVATE or PUBLIC?

dani
  • 3,677
  • 4
  • 26
  • 60
  • Can there be an impact on build time when you pass *unused* include directories to compiler, or *unused* libraries to linker? Exactly this is a difference between *PRIVATE* and *PUBLIC* options for CMake. – Tsyvarev Feb 01 '18 at 20:57

1 Answers1

1

The CMake part mainly reflects your C++ module architecture.

E.g. if you have two libraries and one (B) contains a public class that derives from a class in another one (A), then the header containing class B will include class A. And this will be reflected in a PUBLIC CMake dependency declaration between both.

Optimizing your code/architecture by reducing dependencies has a lot of positive effects incl. compilation time. For discussion on the effects see e.g.:

One of the general topics I'm referring to here is often titled "Reduce C++ Build Times by Reducing Header Dependencies".

Reference

Florian
  • 39,996
  • 9
  • 133
  • 149