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

How do 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

Setting CMAKE_INSTALL_PREFIX from CMakeLists.txt file

CMake developers suggest to use given pattern for change default value of CMAKE_INSTALL_PREFIX inside CMakeLists.txt: # Use this snippet *after* PROJECT(xxx): IF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) SET(CMAKE_INSTALL_PREFIX <path> CACHE PATH <comment> FORCE) ENDIF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) Using that approach # Use this snippet *before* PROJECT(xxx): SET(CMAKE_INSTALL_PREFIX <path> CACHE PATH <comment>) is not recommended: .. solution depends on the implementation details of the … Read more

how to add prebuilt object files to executable in cmake

I’ve done this in my projects with target_link_libraries(): target_link_libraries( myProgram ${CMAKE_CURRENT_SOURCE_DIR}/libs/obj.o ) Any full path given to target_link_libraries() is assumed a file to be forwarded to the linker. For CMake version >= 3.9 there are the add_library(… OBJECT IMPORTED ..) targets you can use. See Cmake: Use imported object And – see also the answer … Read more

How to always run command when building regardless of any dependency?

While I’m not at all pleased with this solution, posting since I stumbled on this page and didn’t see it mentioned. You can add a custom target that references a missing file, eg: if(EXISTS ${CMAKE_CURRENT_BINARY_DIR}/__header.h) message(FATAL_ERROR “File \”${CMAKE_CURRENT_BINARY_DIR}/__header.h\” found, \ this should never be created, remove!”) endif() add_custom_target( my_custom_target_that_always_runs ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/__header.h ) add_custom_command( OUTPUT … Read more

Passing compiler options in CMake

Yes, you can append compiler and linker options. But there are two things you have to differentiate in CMake: the first call to generate the build environment and all consecutive calls for regenerating that build environment after changes to your CMakeLists.txt files or dependencies. Here are some of the possibilities (excluding the more complex toolchain … Read more

Set CFLAGS and CXXFLAGS options using CMake

You need to set the flags after the project command in your CMakeLists.txt. Also, if you’re calling include(${QT_USE_FILE}) or add_definitions(${QT_DEFINITIONS}), you should include these set commands after the Qt ones since these would append further flags. If that is the case, you maybe just want to append your flags to the Qt ones, so change … Read more