How to remove/delete a cmake target?
Asked Answered
M

1

4

In my CMake project I get the following error:

"add_custom_target cannot create target "uninstall" because another target with the same name already exists."

Therefore I would like to remove/delete the already existing target. How can I do that?

The problem is, that I do not write the CMake code that creates these two targets. These two different CMake scripts are downloaded and executed during the execution of my own CMake script and therefore I do not have complete control over that part of the CMake script.

Microparasite answered 19/10, 2021 at 11:43 Comment(1)
A
1

Since those two targets are defined by projects that are not your own (external dependencies), as you said, you don't have much control over them.

There's no way to delete targets at configure time after the point that they are defined, and anyway, the error will always happen at the point of definition when a non-unique target name is attempted to be used.

Someone else had a similar problem here: Isolating gitsubmodule projects in CMake, and indicated in the comments that they were able to resolve their issues by using the Hunter package manager, so perhaps you could give that a try.

What you can also do is reach out to the upstream maintainers of those external dependencies and politely ask them to make their packages more friendly to use-cases like yours by "namespacing" their target names to mitigate name collisions. See also How can I avoid target name collisions when using CMake FetchContent?.

If those project maintainers say no, you could try to work around the problem by patching their CMake files after the download. If you're using the ExternalProject module or the FetchContent module (which is built on top of ExternalProject), you might be able to do this with the PATCH_COMMAND argument (see the docs), which "Specifies a custom command to patch the sources after an update.", or use ExternalProject to to build and install, and then define imported libraries for the installed targets from the external project, which would allow you to control the target names.

Or, if you're feeling impatient and like doing some open-source contributing, you can ask how you can help with this Kitware issue ticket by Craig Scott proposing a CMake feature for Project-level namespaces.

Adis answered 25/1, 2023 at 2:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.