How to print all the properties of a target in cmake?

I have had limited success with the following work-around to the apparent lack of ability to dynamically query for the properties of a target. I invoke the cmake command to list all properties, and then try each one on the target. # Get all propreties that cmake supports if(NOT CMAKE_PROPERTY_LIST) execute_process(COMMAND cmake –help-property-list OUTPUT_VARIABLE CMAKE_PROPERTY_LIST) … Read more

CMake and finding other projects and their dependencies

Easy. Here is the example from the top of my head: The top level CMakeLists.txt: cmake_minimum_required(VERSION 2.8.10) # You can tweak some common (for all subprojects) stuff here. For example: set(CMAKE_DISABLE_IN_SOURCE_BUILD ON) set(CMAKE_DISABLE_SOURCE_CHANGES ON) if (“${CMAKE_SOURCE_DIR}” STREQUAL “${CMAKE_BINARY_DIR}”) message(SEND_ERROR “In-source builds are not allowed.”) endif () set(CMAKE_VERBOSE_MAKEFILE ON) set(CMAKE_COLOR_MAKEFILE ON) # Remove ‘lib’ prefix for … 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: how to change compiler for individual target

NOTE Some have reported that this doesn’t work for them. It definitely used to work for the project I was working on at the time, which was using CMake’s Makefile generator on Ubuntu. It’s possible this technique (hack?) only works with CMake’s Makefile generator, so if you’re using Ninja or another generator, that might be … 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

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

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