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

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

Tell CMake to use C++ compiler for C files coming from CMake?

There are ways to add .c as a valid file extension for the CXX compiler. Even this being very advanced CMake stuff, you may need – if you are bound to support older versions of CMake – a “make rules overwrite script” anyway. So I’ve successfully tested the following: CryptoppMakeRulesOverwrite.cmake list(APPEND CMAKE_CXX_SOURCE_FILE_EXTENSIONS c) CMakeLists.txt cmake_minimum_required(VERSION … Read more

Add Source in a subdirectory to a cmake project

Since CMake 3.1 there is a new way to add source from subdirectories: target_sources Say you have root_dir and root_dir/sub_dir and source files in both. With target_sources you can do this: In root_dir/CMakeLists.txt define the target add_library(some_target main.cpp) add_subdirectory(sub_dir) In root_dir/sub_dir/CMakeLists.txt add sources: target_sources(some_target PRIVATE more_cool_stuff.cpp) some_target will now contain both source files. It is … Read more

CMake target_link_libraries Interface Dependencies

If you are creating a shared library and your source cpp files #include the headers of another library (Say, QtNetwork for example), but your header files don’t include QtNetwork headers, then QtNetwork is a PRIVATE dependency. If your source files and your headers include the headers of another library, then it is a PUBLIC dependency. … Read more