Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

link library to all targets in cmake project

Tags:

cmake

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?

like image 726
Spacemoose Avatar asked Jun 13 '15 13:06

Spacemoose


2 Answers

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

like image 96
fuzzyTew Avatar answered Oct 27 '22 19:10

fuzzyTew


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.

like image 45
Antonio Avatar answered Oct 27 '22 18:10

Antonio