这些目标需要这些dep,如果缺少,请忽略目标
据我所知,CMake中没有这样的功能。
但您可以创建函数,该函数仅在满足依赖关系时添加库/可执行文件。大致如下:
include(CMakeParseArguments)
# Similar as *add_executable*, but additionally accepts DEPENDS option.
# All arguments after this option are treated as libraries targets for link with;
# executable is created only when these targets exist.
function(add_executable_depends name)
cmake_parse_arguments(MY_EXEC "" "" "DEPENDS" ${ARGN})
foreach(depend ${MY_EXEC_DEPENDS})
if(NOT TARGET ${depend})
return()
endif()
endforeach()
add_executable(${name} ${MY_EXEC_UNPARSED_ARGUMENTS})
target_link_libraries(${name} ${MY_EXEC_DEPENDS})
endfunction()
用法示例:
# work out if we can build this target
set(buildLibFOO LibX11_FOUND)
if (${buildLibFOO})
add_library(libFOO ... )
target_link_libraries(libFOO LibX11_LIBRARY)
endif()
# Add executable (and link with libFOO) only when libFOO exists.
add_executable_depends(execBAR ... DEPENDS libFOO)
# always build this one(similar to add_executable)
add_executable_depends(execQUUX ...)