CMake: target_include_directories() prints an error when I try to add the source directory itself, or one of its subdirectories

The origin of the problem is not the target_include_directories command itself, but the attempt to install a target that has a public or interface include directory prefixed in the source path (i.e. the include directory is a subdirectory of your ${PROJECT_SOURCE_DIR}.) While it is perfectly fine and desirable to use absolute paths when building the … Read more

Passing an argument to CMAKE via command prompt

In the CMakeLists.txt file, create a cache variable, as documented here: SET(MY_VARIABLE “option_value” CACHE STRING “Some user-specified option”) Source: https://cmake.org/cmake/help/latest/command/set.html#set-cache-entry Then, either use the GUI (ccmake or cmake-gui) to set the cache variable, or specify the value of the variable on the cmake command line with -D: cmake -DMY_VARIABLE:STRING=option_value2 Modify your cache variable to a … Read more

Adding multiple executables in CMake

My suggestion is to tackle this in two phases: Build a library from the .cpp and .h files, using add_library Iterate through all your .cxx files and create an executable from each, using add_executable and foreach Build the library This could be something as simple as file( GLOB LIB_SOURCES lib/*.cpp ) file( GLOB LIB_HEADERS lib/*.h … Read more

How to list all CMake build options and their default values?

cmake -LAH To list all option(…) and set(… CACHE …) variables, including those marked as advanced, do: cmake -LAH where: -L: list variables -A: include advanced variables. CMake marks most but not all of its default pre-defined variables as, and you can mark you own variables as advanced with mark_as_advanced). Some default CMake advanced variables … Read more

How can I debug CMakeLists.txt files?

There is no interactive debugger for CMake, however there are also the flags -Wdev, –debug-output and –trace which might help. Also remember to check the log files CMakeFiles\CMakeOutput.log and CMakeFiles\CMakeError.log which mainly collect outputs of processes called by CMake (for example while checking for presence of a type or header). Since version 3.7, CMake now … Read more

I have 2 versions of python installed, but cmake is using older version. How do I force cmake to use the newer version?

You may try either of these depending on what you need: For CMake >= 3.12 According to the changelog: New “FindPython3” and “FindPython2” modules, as well as a new “FindPython” module, have been added to provide a new way to locate python environments. find_package(Python COMPONENTS Interpreter Development) Docs: This module looks preferably for version 3 … Read more

Generic rule from makefile to CMake

In CMake you can always declare your own compiler language. So in your case you can e.g. do: cmake_minimum_required(VERSION 2.8) project(MakeBarFromFoo NONE) set( CMAKE_FOO_COMPILE_OBJECT “make_bar_from_foo <SOURCE> <OBJECT>” ) file(GLOB SRCS “*.foo”) add_library(my_rule OBJECT ${SRCS}) set_source_files_properties(${SRCS} PROPERTIES LANGUAGE FOO) Then you can simply work with it as you would with other object library targets. But you … Read more

Why add header files into the add_library/add_executable commands in CMake?

In our projects we use a “simple” way of yours – add_library with both headers and sources. If you add only sources, then you won’t see headers in IDE-generated project. However, when installing, we have to do it like that, using two install commands: install(TARGETS library_name LIBRARY DESTINATION lib) install(FILES ${PUBLIC_HEADERS} DESTINATION include/library_name) If you … Read more