

- #Cmake add library how to#
- #Cmake add library install#
- #Cmake add library generator#
- #Cmake add library software#
To simplify the approach, we won’t install our library in the OS’s standard folder for libraries, neither write tests for the library nor the executable, but always remember to write tests for your software! Also, I’m probably not applying all the concepts of Effective Modern CMake, but I’m struggling to achieve at least a significant percentage of the guidelines. Our project is composed by an executable called app and it uses a static library called lib1 is inside a project’s sub-directory. You can download the example on this GitHub’s repository. To illustrate the concepts, let’s go to a simple example of C++ project.

Hence, you can have more granular control over your modules and can encapsulate its characteristics. You create a target by invoking a command like these (they seem like a constructor, right?):Īnd you customize a target but modifying its properties like these (they seem like setters, right?):Įach command (there’re so many others…) affects one target and can be made PRIVATE (just used by the module), INTERFACE (just used by clients) or PUBLIC (used by the module and its clients).

Target and Properties, remember: Target and Properties One of the benefits of this approach is the possibility to hide from the outside world what is private for the module and what is public and can/should be used by used by its clients. On the CMake jargon, each module gives origin to a target that has a set of properties (ex: compiler definitions, sources, headers, libraries, etc). In the old days of CMake, we are used to inserting a lot of global flags and includes ( include_directories, I’m looking at you) in our CMakeLists.txt that affects every module, there wasn’t any degree of encapsulation and dependency management between projects was a pain.įortunately, things have changed, and now, we’re encouraged on think about modules, for instance: On a project, we have an executable app (a module) that depends on a library lib1 (another module) and we separate what belongs to lib1’s interface ( what it does) and what belongs to lib1’s implementation ( how it does it), more or less, an application of Object Oriented Programming, or more generally, Modular Programming. Instead of reasoning about global flags, structure your project as a graph of modules with explicit dependencies between these modules. You can find more detailed information regarding the concepts of modern CMake on the CMake’s documentation and the talks of Daniel Pfeifer at C++ Now 2017 and Mathieu Ropert at CppCon 2017, guys, you rock! To deal with CMake was not as easy as we would like so, but the set of principles inspired on Modern C++, called Modern (or Effective) CMake has helped us to develop build systems that are easier to:
#Cmake add library generator#
But things are changing and efforts towards the development of a standard, or at least a set of guidelines, are having a positive effect on our daily tasks, aiming to simplify our lives, cool!Īmong the build systems available for C++, one of the most popular is CMake, hold on! Technically, CMake is not a build system, instead, it is a build system generator whose duty is to generate the necessary files that will be used by the build system itself (Make, Ninja, Visual Studio, etc).

#Cmake add library software#
Working with build systems is not the easiest task in the Software Engineering’s world, this is especially true for C++ developers where there’s not a “standard” of to use this or that.
#Cmake add library how to#
How to Use Modern CMake for an App + Lib ProjectĪn example of how to apply some of the so called “Modern CMake” to build a simple project composed by an executable that uses a library.
