首页 > 解决方案 > 添加依赖于所有目标的目标

问题描述

我想使用不同的编译标志构建相同的目标。这个想法是添加两个覆盖目标,在它们上设置相应的属性并添加对 ALL 的依赖。目标是使用不同定义启用/禁用某些功能来构建项目。输出文件列表应保持不变。


add_custom_target(feature1 COMMENT "Build including feature 1")
set_target_properties(feature1 
    PROPERTIES
    COMPILE_FLAGS " -DFEATURE1=1 -DFEATURE2=0 ")
add_dependencies(feature1 ALL)

add_custom_target(feature2 COMMENT "Build including feature 2")
set_target_properties(feature2 
    PROPERTIES
    COMPILE_FLAGS " -DFEATURE1=0 -DFEATURE2=1 ")
add_dependencies(feature2 ALL)

我收到一个错误:目标“feature1”的依赖目标“ALL”不存在。

我该如何解决?还是以其他方式实施?

标签: ccmakebuild-system

解决方案


add_dependencies不接受ALL作为目标。您必须指定要依赖的每个目标:

# Target "depends" depends upon "first_dependency" and "second_dependency"
add_dependencies(depends first_dependency second_dependency)

如果您已经有一个要使用不同选项构建的目标,则标准方法是再次运行配置步骤。但是,如果您不想这样做,一种解决方案是添加具有不同选项的多个目标。您不需要为此添加任何目标依赖项。例如:

# Set the source files we want to build.
set(MY_SOURCES src1.c src2.c etc)

# Add all the targets we need
add_executable(first_target ${MY_SOURCES})
add_executable(second_target ${MY_SOURCES})

# Add the compile options for each target
target_compile_options(first_target PRIVATE option1 option2 etc)
target_compile_options(second_target PRIVATE option3 option4 etc)

现在,在运行配置步骤后,您应该为所需的每组编译选项设置一个目标。

请注意,我从未见过这样做。这看起来像是你可以做的事情,但实际上它可能最终会变得一团糟。


推荐阅读