Making a CMake library accessible by other CMake packages automatically

Taking the code found in a blog post by @daniperez – Use CMake-enabled libraries in your CMake project (III) – I’ve come up with the following minimal solution: myCoolLibrary/CMakeLists.txt cmake_minimum_required(VERSION 3.3) project(myCoolLibrary) function(my_export_target _target _include_dir) file( WRITE “${CMAKE_CURRENT_BINARY_DIR}/${_target}Config.cmake” ” include(\”\$\{CMAKE_CURRENT_LIST_DIR\}/${_target}Targets.cmake\”) set_property( TARGET ${_target} APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES \”${_include_dir}\” ) ” ) export(TARGETS ${_target} FILE “${CMAKE_CURRENT_BINARY_DIR}/${_target}Targets.cmake”) # … Read more

How to detect if current scope has a parent in CMake?

I think the most robust approach is to use the PARENT_DIRECTORY directory property. This will yield the correct answer regardless of whether it’s called before or after the project command, and regardless of whether the parent and child both have the same project name. get_directory_property(hasParent PARENT_DIRECTORY) if(hasParent) message(STATUS “Has a parent scope.”) else() message(STATUS “Doesn’t … 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

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

What is a CMake generator?

What’s a generator? To understand what a generator is, we need to first look at what is a build system. CMake doesn’t compile or link any source files. It used a generator to create configuration files for a build system. The build system uses those files to compile and link source code files. So what’s … Read more

MacOS, CMake and OpenMP

The message basically tells you that you have to provide the path to the libraries and the names of the libraries. The following example should fix your problem (see also find_package(OpenMP)). Note that I use the brew installation using the command “brew install llvm”. The first four lines are just for completeness. set(CMAKE_C_COMPILER “/usr/local/Cellar/llvm/5.0.1/bin/clang”) set(CMAKE_CXX_COMPILER … Read more

CMake: How to add dependency on linker script for executable

You can add a LINK_DEPENDS property to your executable target, using set_target_properties. Add the following line after your add_executable command: set_target_properties(${TARGET_NAME} PROPERTIES LINK_DEPENDS ${LINKER_SCRIPT}) The first argument to set_target_properties is the target name, i.e. the first argument you passed to add_executable.