将生成器表达式与 add_dependency 结合使用

Zac*_*ner 5 cmake

我有一个名为的目标Foo.Compile,它构建我的主程序。

add_executable(Foo.Compile ...)
Run Code Online (Sandbox Code Playgroud)

然后我有一个名为的实用程序目标Foo.Process,它依赖于已经构建的可执行文件并执行一些操作。

add_custom_target(Foo.Process)
add_dependencies(Foo.Process Foo.Compile)
Run Code Online (Sandbox Code Playgroud)

但我只希望它在发布版本中运行,因为它很慢。所以我创建了一个名为 的新目标Foo,这是用户实际构建的。在发布版本中,这应该依赖于Foo.Process,但在其他配置中它不应该依赖。所以我想我可以这样做:

add_custom_target(Foo)
add_dependencies(Foo $<$<CONFIG:Release>:Foo.Process>)
Run Code Online (Sandbox Code Playgroud)

但这给了我一个错误:

  The dependency target "$<$<CONFIG:Release>:Foo.Process>" of
  target "Foo" does not exist.
Run Code Online (Sandbox Code Playgroud)

问题似乎是add_dependencies不支持生成器表达式,所以我想我只需使用target_link_libraries,我知道确实如此。但这给了我一个错误,你不能target_link_libraries在实用程序项目中使用。

请注意,我无法使用,if (CMAKE_BUILD_TYPE == Release)因为我需要支持像 Visual Studio 这样的多配置生成器,所以我认为我必须使用生成器表达式。

我是什么做的?