6

Let me describe what I think is a sufficiently common use case, that it should be supported. Consider a project which consists of a library and a set of executable that use that library. A straightforward approach is to add_library, followed by a sequence of add_executable() target_link_lib() pairs.

This is a lot of boilerplate coding. It would be nice to able to do something like set(PROJECT_LINK_LIBS, lib1 ...), and have cmake remove the extra boilerplate.

Thinking on this more, I realize I would like a link_libraries function that behaves similarly to include_directories. I would argue that this:

  1. Would be useful in a lot of cases.
  2. Would lead to dryer CMakeLists.
  3. Would encourage better code organizations -- there would be a natural incentive to organize the folders, code, and executables in such a way that all executables have the same dependancies -- certainly a clean practice.

Is there anything like this?

Spacemoose
  • 3,856
  • 1
  • 27
  • 48
  • 2
    There is nothing builtin that removes this. However you can develop your own cmake macros and functions to remove some of the boilerplate. – drescherjm Jun 13 '15 at 13:40
  • Possible duplicate of [cmake: add library to all targets](https://stackoverflow.com/questions/50292879/cmake-add-library-to-all-targets) – fuzzyTew Feb 19 '19 at 16:55

2 Answers2

7

As mentioned at https://stackoverflow.com/a/50295894/129550 the requested link_libraries(example example2) function is actually now a part of cmake.

fuzzyTew
  • 3,511
  • 29
  • 24
2

This answer might be obsolete:
Check the new set of variables CMAKE_<LANG>_STANDARD_LIBRARIES

Original Answer: It appears that now a CMAKE_STANDARD_LIBRARIES variable exists, where you can append the libraries according to your need. However, this variable seemingly expect full path to the libraries.

See here.

Antonio
  • 19,451
  • 13
  • 99
  • 197