How to pass command-line arguments in CTest at runtime

I’ve figured out a way to do it (using the Fundamental theorem of software engineering). It’s not as simple as I’d like, but here it is. First, create a file ${CMAKE_SOURCE_DIR}/cmake/RunTests.cmake with the content if(NOT DEFINED ENV{TESTS_ARGUMENTS}) set(ENV{TESTS_ARGUMENTS} “–default-arguments”) endif() execute_process(COMMAND ${TEST_EXECUTABLE} $ENV{TESTS_ARGUMENTS} RESULT_VARIABLE result) if(NOT “${result}” STREQUAL “0”) message(FATAL_ERROR “Test failed with return value … Read more

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

CMake: target_link_libraries include as SYSTEM to suppress compiler warnings

I defined a function to handle this for me: function(target_link_libraries_system target) set(libs ${ARGN}) foreach(lib ${libs}) get_target_property(lib_include_dirs ${lib} INTERFACE_INCLUDE_DIRECTORIES) target_include_directories(${target} SYSTEM PRIVATE ${lib_include_dirs}) target_link_libraries(${target} ${lib}) endforeach(lib) endfunction(target_link_libraries_system) I can now call target_link_libraries_system(myapp lib::lib) and the include directories are read from the target’s properties. This can be extended to optionally specify the PUBLIC|PRIVATE|INTERFACE scope: function(target_link_libraries_system target) set(options … Read more

CMake install (TARGETS in subdirectories)

According to this bugreport, install(TARGETS) command flow accepts only targets created within the same directory. So you need either move the add_library() call into the top-level directory, or split install(TARGETS) call into per-target ones, and move each of them into the corresponding subdirectory. Since CMake 3.13 install(TARGETS) can work even with targets created in other … Read more

CMake not generating compile_commands.json

This ended up being an issue with using an old version of CMake. I ended up installing the newest version and it worked as expected. According to Clang docs “Currently CMake (since 2.8.5) supports generation of compilation databases for Unix Makefile builds (Ninja builds in the works) with the option CMAKE_EXPORT_COMPILE_COMMANDS.”

target_compile_options() for only C++ files?

Solution You can do this with generator expressions: target_compile_options(MyLib PUBLIC $<$<COMPILE_LANGUAGE:CXX>:-std=c++14>) Alternative But the more platform independent way of doing it in this particular case would be to use target_compile_features(). I’m not sure which compiler feature you’re using, so the following is only an example: target_compile_features(MyLib PUBLIC cxx_explicit_conversions)

cmake cannot find libraries installed with vcpkg

You need to install the packages beforehand (using vcpkg install ). (Then you could specify the toolchain as a CMake option: -DCMAKE_TOOLCHAIN_FILE=C:\path\to\vcpkg\scripts\buildsystems\vcpkg.cmake but this won’t work if you already specify a toolchain, such as when cross-compiling.) “include” it, instead, to avoid this problem: Add this line to the project CMakeLists.txt before find_package(): include(/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake)

Where is the CMake GUI for Linux?

Update: As of CMake 3.7.2, cmake-gui is still not built by default, but can easily be added to the build by specifying one additional flag. Qt is still required, I am using 4.8 but I’m sure other versions will work fine. Download the source from the website, extract to a directory of your choosing, then … Read more

CMAKE_PREFIX_PATH doesn’t help CMake in finding Qt5

I installed the following missing packages: sudo apt-get install qtbase5-dev sudo apt-get install qtdeclarative5-dev Attaching any kind of prefix is not required now: CMakeList: :~/junk/qtquick_hello_cmake$ cat CMakeLists.txt cmake_minimum_required(VERSION 2.8.12) project(qtquick_hello_cmake) set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) find_package(Qt5 COMPONENTS Quick Core REQUIRED) qt5_add_resources(RESOURCES qml.qrc) add_executable(${PROJECT_NAME} “main.cpp” “qml.qrc”) qt5_use_modules(${PROJECT_NAME} Quick Core) target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Quick) New output: :~/junk/qtquick_hello_cmake$ … Read more