假设我有一个库 ProjectA
,我在我的 CMakefile 中定义了一个自定义目标 build-tests
来构建我的测试,这样我就可以做 make test
之后。 (参见 CMake & CTest : make test doesn't build tests)。
然后,我有第二个库 ProjectB
,它依赖于 ProjectA
。我使用 git submodule 这样我就可以在 ProjectB
的 CMakefile 中执行 add_subdirectory(ProjectA)
。问题是,我想做同样的事情:定义一个自定义目标 build-tests
来构建 ProjectB
的测试,但我不能,因为它已经在项目A
...
我的问题类似于How to handle a transitive dependency conflict using Git submodules and CMake?和 CMake and using git-submodule for dependence projects但他们可以定义一个目标来避免这种情况,而我希望能够在 ProjectA
和 ProjectB
中进行 make build-tests
。
那么,有没有办法在本地为 cmake 项目定义自定义目标?还是有一种聪明的方法来做我想做的事? (这看起来很自然)
最佳答案
So, is there a way to define a custom target locally to a cmake project for example ? or is there a clever way to do what I would like to do ? (which seems quite natural)
因为您正在使用自定义目标,可能是 ALLOW_DUPLICATE_CUSTOM_TARGETS
可以胜任这项工作。
文档确实非常清楚:
Allow duplicate custom targets to be created.
如果你走得更远一点,你会发现:
The property allows multiple
add_custom_target
command calls in different directories to specify the same target name.
不幸的是它也有一些限制:
[...] setting this property will cause non-Makefile generators to produce an error and refuse to generate the project.
仔细阅读文档(参见上面的链接)以了解它是否适合您的目的。
关于c++ - CMake : several projects using custom target to make test,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45065290/