CMake target_link_libraries Dependencies

I’m new to CMake and am attempting to figure out what the link requirements for ‘target link libraries’ are (Public,Private,Interface). I understand the concepts of public, private, and Interface dependencies, but I’m not sure how to apply them to my project.

I have a number of libraries in my project, and some of them have dependencies on each other.For instance, I have a logger library that is utilised in other libraries and is also used in the final application executable.

Reference:
image

So lib_storage is reliant on logger, and App A is reliant on both lib_storage and logger. If I make logger a public link to lib_storage, App A will have access to logger as well. However, removing lib_storage in the future will result in a logger build fault for App_A. So, how do you attach logger to lib_storage and logger to App A correctly?

If App_A needs the logger itself, it should directly link regardless of whether a dependency brings it in as well.

The logic is:

  • if you use a target, link it no matter what
  • if you are header-only, use INTERFACE
  • if using you implies using a target (e.g., you include a header from it in your public headers), use PUBLIC
  • (corner cases that probably don’t apply here that I don’t have the time to explain)
  • otherwise use PRIVATE
1 Like