Skip to content

CMake

This section groups helpful information about CMake best practices.

The ${APPLE} CMake variable

This variable identifies if the running system is a MAC. This is useful in test covergage. In Linux we use the flag -lgcov while in MAC we use --coverage.

If you are using shared libraries, you will likely need to set these variables.

Dynamic libraries are linked during running time. That means that the executable needs to find the libraries it needs to use. This duty is delegated to ld in UNIX systems. By default, the system searches for the requested library in the following sequence of directories:

  • RPATH
  • LD_LIBRARY_PATH
  • RUN_PATH

By setting CMAKE_INSTALL_RPATH you store the RPATH the executable has to look at it.

More details (here).

Setting default values for CMAKE_BUILD_TYPE and CMAKE_INSTALL_PREFIX

Set up custom FIND_PACKAGES

CMake comes with pre-defined Find\<package_name>.cmake files. That is the case for FindBoost.cmake, for example. To see a list of these files, type

cmake --help-module-list

The .cmake files are located in: /usr/share/cmake-3.16/Modules.

Set up to use gcov and lcov

Compile test executable with

  • -fprofile-arcs
  • -ftest-coverage

And then link with -lgcov

Alternatively, compile with

  • --coverage