install(EXPORT) complains that a dependency is in multiple export sets

I have a project, Foo, which depends on another project, Bar. Bar creates multiple targets, which I add to a single export set via a macro thusly:

add_library(Bar::${TARGET_NAME} ALIAS ${TARGET_NAME})

# do things like target_include_directories(...)

    EXPORT Bar

install(EXPORT Bar
    FILE BarConfig.cmake

In project Foo, I add Bar with FetchContent and create an export set for Foo as well, thusly:

install(EXPORT Foo
    FILE FooConfig.cmake

However, I get an error:

CMake Error:  install(EXPORT "Foo" ...) includes target "Foo-lib" which requires target "Bar-lib" that is not in this export set, but in multiple other export sets:


In this error, it lists many many locations where BarConfig.cmake definitely does not exist:

  • /usr/local/lib/cmake/Bar/Bar.cmake
  • /usr/local/lib/cmake/Bar/BarConfig.cmake
  • and on and on, something like ~10-15 locations

I have no idea what’s going on here, or why it thinks there are so many Bar export sets.

1 Like

Do you install(EXPORT) in the macro for each target? It should be done only once after all of the targets have been added to it (effectively “closing” the export set).

Does Bar do its own target exporting? If so, you may need to suppress that.

Sorry, I got pulled onto a different problem at work and completely forgot about this thread. I was in fact calling the macro for each target, and Bar was also doing its own target exporting. Thanks for the assistance many moons ago!