How to handle a transitive dependency conflict using Git submodules and CMake?

There are several approaches for detect and discard inclusion of the project, which has already be included in some other parts of the main project. Check project’s target existence The simplest pattern for single inclusion of subproject is checking existence of some subproject’s target: # When include ‘C’ subproject if(NOT TARGET library_C) add_subdirectory(C) endif() (Here … Read more

Linking GLEW with CMake

Typical CMake scripts like FindGLEW will define variables that specify the paths and files that your project needs. If the script can’t automatically identify the correct paths (usually because of nonstandard install location, which is fine), then it leaves these variables up to you to fill in. With command line CMake, you use the -D … Read more

CMake compare to empty string with STREQUAL failed

You ran into a rather annoying “it’s not a bug, it’s a feature” behavior of CMake. As explained in the documentation of the if command: The if command was written very early in CMake’s history, predating the ${} variable evaluation syntax, and for convenience evaluates variables named by its arguments as shown in the above … Read more

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

How do I change the startup project of a Visual Studio solution via CMake?

CMake now supports this with versions 3.6 and higher through the VS_STARTUP_PROJECT directory property: cmake_minimum_required(VERSION 3.6) project(foo) # … add_executable(bar ${BAR_SOURCES}) set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT bar) This will set bar as the startup project for the foo.sln solution.

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