How to check whether a target has been added or not?
Use the TARGET clause of the if command: conditionally_add (mylib mysrc.cc ${some_condition}) if (TARGET mylib) # Do something when target found endif()
Use the TARGET clause of the if command: conditionally_add (mylib mysrc.cc ${some_condition}) if (TARGET mylib) # Do something when target found endif()
You have to pass the arguments as a second option like this: cmake_minimum_required (VERSION 2.8) set (git_cmd “git”) set (git_arg “–version”) message(STATUS “git cmd: ${git_cmd}”) execute_process(COMMAND ${git_cmd} ${git_arg} WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} RESULT_VARIABLE git_result OUTPUT_VARIABLE git_ver) message(STATUS “git ver[${git_result}]: ${git_ver}”)
You want target_compile_definitions instead of set_target_properties: target_compile_definitions(trie_io_test PRIVATE UNIT_TESTING=1 IO_TEST=1)
AFAIK, there is no built-in support for something like this, but you could certainly write it yourself: if(CMAKE_CXX_COMPILER_ID STREQUAL “GNU”) if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS “your.required.gcc.version”) message(FATAL_ERROR “Insufficient gcc version”) endif() elseif(CMAKE_CXX_COMPILER_ID STREQUAL “MSVC”) if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS “your.required.msvc.version”) message(FATAL_ERROR “Insufficient msvc version”) endif() elseif(…) # etc. endif() However, I suggest you actually consider a feature-detection approach instead. That is, … Read more
How do I print the result of the evaluated generator expression during configuration? You cannot. Generator expressions are intended for things, which are not exactly known at configuration stage: they depend on build type, which, in case of multiconfiguration generators, becomes known only at the build stage. You may, however, save a value of the … Read more
There is no CMake debugger or similar¹. What you can do is: Read the output of CMake, sometimes it already gives hints like “missing INCLUDE_MYLIB_DIR”). Delete you CMakeCache.txt and/or remove the build directory, to be sure you don’t miss output because the result was cached. Repeat and check whether caching had an influence. You get … Read more
In current CMake releases: After some error checking add_dependencies results in a call to Target->AddUtility(). x is added to the list of utilities for my-lib. target_link_libraries does not result in a call to AddUtility, but it does add the arguments to the LINK_LIBRARIES target property. Later, both the content of the LINK_LIBRARIES target property and … Read more
CMake doesn’t allow to install IMPORTED libraries as TARGETS. Use install(FILES) instead. There are at least 2 reasons for such behavior: Š”itation of one of CMake developer from bug report Imported targets were originally designed for importing from an existing installation of some external package so installing did not make sense at the time. When … Read more
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
From the documentation, I don’t get the difference between the two variables. The difference is that CMAKE_PROJECT_NAME is the name from the last project call from the root CMakeLists.txt, while PROJECT_NAME is from the last project call, regardless from the location of the file containing the command. The difference is recognizable from the following test. … Read more