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

How do I get the parent directory path of a path in CMake?

For CMake 3.20 and newer, the recommended way is to use cmake_path with a PARENT_PATH option: cmake_path(GET MYPROJECT_DIR PARENT_PATH PARENT_DIR) For older versions up to CMake 2.8.12, use the get_filename_component command with the DIRECTORY option: get_filename_component(PARENT_DIR ${MYPROJECT_DIR} DIRECTORY) For versions of CMake older than 2.8.12, use the PATH option: set (MYPROJECT_DIR /dir1/dir2/dir3/myproject/) get_filename_component(PARENT_DIR ${MYPROJECT_DIR} PATH)

How can I decide what to put in my CMakeList.txt’s cmake_minimum_required call?

The cmake_minimum_required() function is used to avoid any cryptic error messages due to the CMakeLists.txt assuming a later version of CMake than the one installed on the current host. As an example, failing early, and with a clear message… CMake 3.2 or higher is required. You are running version 2.8.12.2 …is to be preferred over … Read more

How do I specify relative paths in CMAKE?

The is almost exactly the same, except that you have allow for CMake to run in another location for “out-of-tree” builds. You do this by specifying the path relative to ${CMAKE_CURRENT_SOURCE_DIR}, which is the directory containing the current CMakeLists.txt file. ${CMAKE_CURRENT_SOURCE_DIR}/../../External/Library But, you might want to reconsider, and instead use FIND_LIBRARY() and FIND_FILE() commands to … Read more

CMake generator expression is not evaluated

Generator expressions are not evaluated at configure time (when CMake is parsing CMakeLists, executing commands like add_target() or message() etc.). At this time, a generator expression is just a literal string – the character $ followed by <, then T, then … Evaluation of generator expressions happens at generate time (that’s why they are called … Read more

CMake rejects a second target_link_libraries talking about “keyword” vs “plain” [duplicate]

Your use of target_link_libraries() is indeed problematic, as it involves two different “flavors” of this command, a traditional and a newer one. In one use, you specify the dependency is PRIVATE; in the other, you specify nothing. That’s not acceptable: Either you specify PUBLIC/PRIVATE/INTERFACE for all elements, or for none. So, you can fix your … Read more

Optional Argument in cmake macro

Any arguments named in your macro definition are required for callers. However, your macro can still accept optional arguments as long as they aren’t explicitly named in the macro definition. That is, callers are permitted to pass in extra arguments (which were not named in the macro’s argument list). Your macro can check for the … 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