When should I quote CMake variable references?

Two principles of CMake you have to keep in mind: CMake is a script language and arguments are evaluated after the variables are expanded CMake differentiates between normal strings and list variables (strings with semicolon delimiters) Examples set(_my_text “A B C”) with message(“${_my_text}”) would give A B C set(_my_list A B C) with message(“${_my_list}”) would … Read more

Passing compiler options in CMake command line

Yes, you can append compiler and linker options. But there are two things you have to differentiate in CMake: the first call to generate the build environment and all consecutive calls for regenerating that build environment after changes to your CMakeLists.txt files or dependencies. Here are some of the possibilities (excluding the more complex toolchain … Read more

Specifying build directory within CMakeLists file

Afaik, with CMake the build directory is always the directory from where you invoke the cmake or ccmake command. So if you want to change the build directory, you have to change directories before running CMake. To control the location where executables, static and shared libraries are placed once finished, you can modifiy CMAKE_RUNTIME_OUTPUT_DIRECTORY, CMAKE_ARCHIVE_OUTPUT_DIRECTORY, … Read more

‘std::filesystem’ has not been declared after including

It seems your C++17 compiler doesn’t include the standard filesystem header. One possible way to get around that: #ifndef __has_include static_assert(false, “__has_include not supported”); #else # if __cplusplus >= 201703L && __has_include(<filesystem>) # include <filesystem> namespace fs = std::filesystem; # elif __has_include(<experimental/filesystem>) # include <experimental/filesystem> namespace fs = std::experimental::filesystem; # elif __has_include(<boost/filesystem.hpp>) # include <boost/filesystem.hpp> … Read more

CMake : Changing name of Visual Studio and Xcode exectuables depending on configuration in a project generated by CMake

Take a look at the list of target properties: One of those is the OUTPUT_NAME and OUTPUT_NAME_<CONFIG>. The last one can be set for each config-type (Debug, Release, MinSizeRel, etc.) You can set them on your program with set_target_properties, e.g.: project( YourProject ) add_executable( myprogram ${YourSources} ) set_target_properties( myprogram PROPERTIES OUTPUT_NAME_DEBUG program-debug ) set_target_properties( myprogram … Read more

Postpone making custom target until install

A possible solution is to have the make install command invoke the make tgt as a side effect. This can be done by using the CODE signature of the install command: add_custom_command(OUTPUT somefile) add_custom_target(tgt DEPENDS somefile) install(CODE “execute_process(COMMAND \”${CMAKE_COMMAND}\” –build \”${CMAKE_CURRENT_BINARY_DIR}\” –target tgt)”) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/somefile DESTINATION somedir) The execute_process invokes cmake to build the target … Read more

Retrieve all link flags in CMake

As @Tsyvarev has commented there is no build-in command or property “to programmatically retrieve the complete list of linker flags” in CMake. But inspired by your hint “so I’d like to write out the link line necessary for building these executables to a data file” I think I found a feasible solution (at least for … Read more

How to tell CMake to use relative paths

CMake does always use absolute paths. It’s part of the concept. Therefore you can’t move the generated build environment files nor can you e.g. bring them under source control or make the verbose output prettier (you could just play a little with the rule messages like here). There once was CMAKE_USE_RELATIVE_PATHS, but the documentation reveals: … Read more

Check CMake Cache Variable in Toolchain File

I don’t pretend to fully understand what’s going on behind the scenes, but here’s a workaround that works for me: # Problem: CMake runs toolchain files multiple times, but can’t read cache variables on some runs. # Workaround: On first run (in which cache variables are always accessible), set an intermediary environment variable. if (FOO) … Read more

Unable to find Eigen3 with CMake

Turning my comment into an answer The find package scripts – like FindEigen3.cmake – normally use the find_path() command to detect the package’s include directory (see it’s documentation for the full details). FindEigen3.cmake uses the following code snippet: find_path(EIGEN3_INCLUDE_DIR NAMES signature_of_eigen3_matrix_library PATHS ${CMAKE_INSTALL_PREFIX}/include ${KDE4_INCLUDE_DIR} PATH_SUFFIXES eigen3 eigen ) So it looks in CMAKE_INSTALL_PREFIX which on … Read more