Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In CMake how to create targets with identical names?

Tags:

cmake

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?

like image 465
Nikitha Avatar asked Jun 22 '17 05:06

Nikitha


People also ask

How do I set a target on CMake?

set_target_properties(target1 target2 ... PROPERTIES prop1 value1 prop2 value2 ...) Sets properties on targets. The syntax for the command is to list all the targets you want to change, and then provide the values you want to set next.

What are target properties in CMake?

Properties are usually used to control how a target is built, but some query the target instead. This command can get properties for any target so far created. The targets do not need to be in the current CMakeLists. txt file.

What is a custom target?

A custom target is a software package that you create to deploy MATLAB® and Simulink® designs to target hardware. Using the package, you can perform tasks to optimize, prototype, verify, and deploy an application to your hardware. This table lists tasks that you can perform by using target classes and functions.

What are targets in CPP?

Targets are much like “objects” in other languages; they have properties (member variables) that hold information. The SOURCES property, for example, will have simple. cpp in it.


1 Answers

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.

like image 95
skypjack Avatar answered Sep 18 '22 15:09

skypjack