Cmake tips

  • When adding an install target (so that your library/file is installed during “make install"), you use:
    install(TARGETS <target> DESTINATION <path>).
    <target> is what you used in your add_library (or add_executable).
    The <path> is somewhat tricky. If the path is not absolute, then cmake will automatically add CMAKE_INSTALL_PREFIX to the generated cmake_install file.
    For example:
    install(TARGETS mylib DESTINATION "path/to/lib") will result in: FILE(INSTALL DESTINATION "${CMAKE_INSTALL_PREFIX}/path/to/lib" ... inside the cmake_install.cmake file.
    A common mistake is to do: install(TARGETS mylib DESTINATION "${CMAKE_INSTALL_PREFIX}/path/to/lib")
    which will generate: FILE(INSTALL DESTINATION “/whatever-CMAKE_INSTALL_PREFIX-was-during-cmake/path/to/lib” which is not what you want (it will use the CMAKE_INSTALL_PREFIX value at cmake time and “hardcode” it inside cmake_install.cmake). If, then, you run something like:
    cmake install -DCMAKE_INSTALL_PREFIX=new/path -P cmake_install.cmake
    it will not use the new/path.
  • When explicitly specifying library names in CMakeLists or via -D flag in the cmake command line, here is something to keep in mind:
    The actual library file name might be something like libfoo.so (or libfoo.dylib) but the name you specify in the -l option is without the “lib” prefix, like so: -lfoo. If you specify -llibfoo it won’t find it.

Cmake tips