In CMake how to create targets with identical name

2019-06-28 06:58发布

问题:

I have a question regarding CMake and I need help to solve the following error I'm getting:

CMake Error at :::: (add_custom_target):
  add_custom_target cannot create target "generate" because another target
  with the same name already exists.  The existing target is a custom target
  created in source directory :::::.

Here the target names of the two same level CMakeLists.txt are the same and I want to keep them identical, without any conflict. Can anyone help me out?

回答1:

According with CMake policy CMP0002 (introduced by CMake 2.6, emphasis mine):

Targets names created with add_executable, add_library, or add_custom_target are logical build target names. Logical target names must be globally unique [...]

The following note deserves a mention and could probably help you anyway:

Custom targets must simply have globally unique names (unless one uses the global property ALLOW_DUPLICATE_CUSTOM_TARGETS with a Makefiles generator).

It means that there exists a global property named ALLOW_DUPLICATE_CUSTOM_TARGETS that is probably what you are looking for. It has a limited use and you should read carefully the documentation, but it's worth a try.
The most relevant part follows:

Makefile generators are capable of supporting duplicate custom target names. [...] However, setting this property will cause non-Makefile generators to produce an error and refuse to generate the project.

To be able to use duplicate custom targets put the following line in your CMakeLists.txt:

set(ALLOW_DUPLICATE_CUSTOM_TARGETS TRUE)

If it solves your issue mainly depends on the actual problem, so I cannot say.



回答2:

This could be a good help:

OUTPUT_NAME sets the real name of a target when it is built and can be used to help create two targets of the same name even though CMake requires unique logical target names.

https://cmake.org/cmake/help/v3.0/command/set_target_properties.html



标签: cmake