Add cmake in two different libs, cmake error

I want to add libtorch to two existing projects. So I add:

find_package(torch REQUIRED)

in both cmake files, which gives me the following error:

add_library cannot create target “torch_library” because another target
with the same name already exists. The existing target is an interface
library created in source directory

How do I correctly add it to both projects?

So the above hints at CMake seeing both find_package statements in one invocation.
If it’s in the same CMake project, I think you can use find_package once and then use the torch “environment” set up by CMake for both.

Best regards


Tried this, however then torch/torch.h is not found in the other project :confused:

So I can have it at any one of them at each time without any problems. However, I get issues when I try to have it in both projects.

So you’re trying to make them work separately as well as “in combination”?
One thing you could try is to make the find_package conditional on if (NOT TARGET torch_library) or somesuch to have it executed exactly once.
If that doesn’t solve it, maybe it would help if you showed a bit about your CMake setup.

Best regards


Thanks you, this can be marked as sloved.
Tried your suggestion but it did not work, this is a huge cmake-project with about 800 cpp different projects. However, I managed to get it working in the end.

@Jesper.Lindberg we’re running into the same issue. How did you get it t work?

Sorry for late reply but I won’t be able to help you with this issue as I did not solve it myself but rather got help from a colleague who specializes in cmake builds.

Does your colleague have a pytorch discuss account? :smiley: Or maybe a github ID?

Sorry, I am afraid not. :frowning: