c - 添加依赖于所有目标的目标
问题描述
我想使用不同的编译标志构建相同的目标。这个想法是添加两个覆盖目标,在它们上设置相应的属性并添加对 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”不存在。
我该如何解决?还是以其他方式实施?
解决方案
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)
现在,在运行配置步骤后,您应该为所需的每组编译选项设置一个目标。
请注意,我从未见过这样做。这看起来像是你可以做的事情,但实际上它可能最终会变得一团糟。
推荐阅读
- swift - iOS | SwiftUI | 通过 ContentView 在 TimerView 和 TaskView 之间交换项目的不良结果,@State 未提供结果
- gtk3 - 使用 Glade 和 Python 的 GTK3 背景图像
- java - 使用 JSch 登录后从交互式菜单中选择
- angular - 在离子项目中必须混入哪些文件/文件夹
- java - 带有 Spring Kafka 和 Avro 的模式注册表
- javascript - 删除事件侦听器未按预期工作
- c# - 监听 HTTP 和 HTTPS?- C#/ASP.NET/Kestrel
- ruby-on-rails - 无论输入如何,Rspec 测试始终通过
- python - 在 MySQLdb 游标的 SQL 语句中使用 %%s%
- python - Python列出自定义基数系统中的前n个条目