-1

When using target_link_libraries with a not too old version of CMake, I am encouraged to use one of the key words PRIVATE, PUBLIC, or INTERFACE. Let's assume I just want to get the linking done and I don't want to understand the implications of my choice. I just want my executable getting linked to the library.

  • What would be a sane default choice?
  • How do I know, that I should start to understand the meaning of the key words? With other words, what kinds of errors do occur, if I chose (by sane default or by plain mistake) the wrong one?
usr1234567
  • 21,601
  • 16
  • 108
  • 128
  • @StephenNewell That question has a great answer. Thanks for linking it here. But it explains the meaning of the key words. I want to have a default without further thinking. – usr1234567 Apr 01 '21 at 20:12
  • 1
    "I just want my **executable** getting linked to the library." - Since an **executable** cannot be linked **into** another executable or a library, then for an executable `INTERFACE` linkage has no sense, and `PUBLIC` and `PRIVATE` linkage give the same effect. – Tsyvarev Apr 01 '21 at 20:50
  • 1
    "I don't want to understand the implications of my choice" - Such a lazy reason. Currently I treat the [question](https://stackoverflow.com/questions/26037954/cmake-target-link-libraries-interface-dependencies) to be a proper duplicate of yours one. Even if you want your question to be specific for the executable linkage, an additional **answer** to the duplicate question looks better than (yours) additional question. – Tsyvarev Apr 01 '21 at 20:55
  • 1
    *"What would be a sane default choice?"* It's not to hard to figure out. For executables just don't use `INTERFACE`; for libs as a rule of thumb just check the public headers of your lib: are headers of the linked lib mentioned there? Then go with `PUBLIC` unless the header is not involved in the compilation of the lib itself; in that case use `INTERFACE` instead. Otherwise use `PRIVATE`. The wrong choice could result in unexpected conflicts in symbols/headers or symbols or headers not being available unless you link the dependency of your lib to a lib linking yours "manually". – fabian Apr 01 '21 at 22:29

1 Answers1

1

What would be a sane default choice?

PUBLIC

You can also just do target_link_libraries(the_target the_lib).

How do I know, that I should start to understand the meaning of the key words? With other words, what kinds of errors do occur, if I chose (by sane default or by plain mistake) the wrong one?

Missing include header error. Undefined reference or unresolved external symbol error.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111