# add\_library in a -config.cmake causing problem

**URL:** <https://discourse.cmake.org/t/add-library-in-a-config-cmake-causing-problem/13122>\
**Category:** Code\
**Created:** [December 4, 2024, 2:51pm UTC](https://discourse.cmake.org/t/add-library-in-a-config-cmake-causing-problem/13122 "2024-12-04T14:51:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdorier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/eb9ed0/32.png) [@mdorier](https://discourse.cmake.org/u/mdorier)\
**Post date:** [December 4, 2024, 2:51pm UTC](https://discourse.cmake.org/t/add-library-in-a-config-cmake-causing-problem/13122/1 "2024-12-04T14:51:11Z")

</div>

I have three cmake-based projects: Flock, Bedrock, and Mofka. Mofka depends on Flock and Bedrock. Bedrock depends on Flock. Flock has the following in flock-config.cmake.in (which is configured and installed as flock-config.cmake):

```auto
...
add_library (flock::server ALIAS flock-server)

```

Bedrock uses `find_package(flock REQUIRED)` to find Flock, and builds fine, linking its library with `target_link_libraries(bedrock PRIVATE flock::server)`. The bedrock-config.cmake.in has this:

```auto
find_dependency (flock)

```

In the Mofka project I also have `find_package (bedrock REQUIRED)` and `find_package (flock REQUIRED)`, but cmake gives me the following error:

```auto
CMake Error at [...]/share/cmake/flock/flock-config.cmake:49 (add_library)
add_library cannot create ALIAS target "flock::server" because another target with the same name already exists.

```

I am under the impression that CMake loads `flock-config.cmake` twice (once when doing find\_package(flock) and once when doing find\_package(bedrock)), causing this `add_library` to be processed twice. Is my diagnosis correct and if so, how to I fix this? Is calling `add_library` in a -config.cmake file not correct?

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [December 4, 2024, 3:02pm UTC](https://discourse.cmake.org/t/add-library-in-a-config-cmake-causing-problem/13122/2 "2024-12-04T15:02:14Z")

</div>

It sounds like you are creating the `*-config.cmake` files manually, which of course works too, but I would probably recommend you to rely on [standard CMake helpers](https://cmake.org/cmake/help/latest/module/CMakePackageConfigHelpers.html) for auto-generating those.

Anyway, the problem you are experiencing might be due to the fact that when CMake auto-generates those configs and targets files, this is how library targets are defined there, for example:

```cmake
add_library(some::thing STATIC IMPORTED)

```

so maybe your config is missing this `IMPORTED` argument?

Also you should probably add `CONFIG` argument to your `find_package()` calls, since you are providing [CMake configs](https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html#using-packages) for those libraries.

---

<div class="post-metadata">

**Author:** ![mdorier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/eb9ed0/32.png) [@mdorier](https://discourse.cmake.org/u/mdorier)\
**Post date:** [December 4, 2024, 3:29pm UTC](https://discourse.cmake.org/t/add-library-in-a-config-cmake-causing-problem/13122/3 "2024-12-04T15:29:45Z")

</div>

The following files are generated by cmake: flock-config-version.cmake, flock-config.cmake, flock-targets-release.cmake, flock-targets.cmake.

They are generated as follows:

```auto
  # installation stuff (packaging and install commands)
  write_basic_package_version_file (
      "flock-config-version.cmake"
      VERSION ${FLOCK_VERSION}
      COMPATIBILITY AnyNewerVersion)

  # generate our config file for find_package()
  set (INCLUDE_INSTALL_DIR include/)
  set (LIB_INSTALL_DIR lib/)
  configure_package_config_file (flock-config.cmake.in
    ${CMAKE_CURRENT_BINARY_DIR}/flock-config.cmake
    INSTALL_DESTINATION ${LIB_INSTALL_DIR}/flock/cmake
    PATH_VARS INCLUDE_INSTALL_DIR)

  # "make install" rules
  install (TARGETS flock-server
           EXPORT flock-targets
           ARCHIVE DESTINATION lib
           LIBRARY DESTINATION lib)
  install (EXPORT flock-targets
           DESTINATION ${flock-pkg}
           FILE "flock-targets.cmake")
  install (FILES "${CMAKE_CURRENT_BINARY_DIR}/flock-config.cmake"
                 "${CMAKE_CURRENT_BINARY_DIR}/flock-config-version.cmake"
           DESTINATION ${flock-pkg} )

```

The `flock-config.cmake.in` is as follows:

```auto
  set (FLOCK_VERSION @FLOCK_VERSION@)

  @PACKAGE_INIT@

  set_and_check (FLOCK_INCLUDE_DIR "@PACKAGE_INCLUDE_INSTALL_DIR@")

  check_required_components (flock)

  include (CMakeFindDependencyMacro)
  find_dependency (PkgConfig)
  pkg_check_modules (margo REQUIRED margo)
  pkg_check_modules (json-c REQUIRED json-c)

  include ("${CMAKE_CURRENT_LIST_DIR}/flock-targets.cmake")

  add_library (flock::server ALIAS flock-server)

```

Flocks’ CMakeLists.txt has `add_library (flock::server ALIAS flock-server)`, but I looked into the generated flock-targets.cmake and it `flock::server` doesn’t show up, only `flock-server`, that’s why I initially added it to `flock-config.cmake.in`.

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [December 4, 2024, 3:39pm UTC](https://discourse.cmake.org/t/add-library-in-a-config-cmake-causing-problem/13122/4 "2024-12-04T15:39:42Z")

</div>

> added it to `flock-config.cmake.in`

That looks like the source of the problem. If you are relying on auto-generated configs/targets, then all the required `add_library()` statements should be added there automatically.

---

<div class="post-metadata">

**Author:** ![mdorier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/eb9ed0/32.png) [@mdorier](https://discourse.cmake.org/u/mdorier)\
**Post date:** [December 4, 2024, 3:43pm UTC](https://discourse.cmake.org/t/add-library-in-a-config-cmake-causing-problem/13122/5 "2024-12-04T15:43:08Z")

</div>

The thing is I don’t know how to do this differently. If I add `flock::server` to the `install(TARGETS ...)` line, I get this error:

```auto
CMake Error at src/CMakeLists.txt:110 (install):
  install TARGETS given target "flock::server" which is an alias.

```

EDIT: also here [https://cmake.org/cmake/help/latest/command/add\_library.html#alias-libraries](https://cmake.org/cmake/help/latest/command/add_library.html#alias-libraries) I read " An `ALIAS` target may not be installed or exported.", so how can I make `flock::server` available?

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [December 4, 2024, 4:54pm UTC](https://discourse.cmake.org/t/add-library-in-a-config-cmake-causing-problem/13122/6 "2024-12-04T16:54:21Z")

</div>

Ah, that should be trivial - just add `NAMESPACE` argument, like this:

```cmake
install(EXPORT "flock-targets"
    FILE "flock-targets.cmake"
    NAMESPACE "flock::"
    DESTINATION "${flock-pkg}" # assuming this path is correct
)

```

For example, in [this project](https://github.com/retifrav/cmake-target-link-libraries-example/tree/master/dpndnc) the resulting auto-generated `ThingyTargets.cmake` will contain the following:

```cmake
# ...

# Create imported target dpndnc::Thingy
add_library(dpndnc::Thingy STATIC IMPORTED)

# ...

```

Although since your target name is `flock-server`, it will then become `flock::flock-server`, so then you’d probably want to rename it to just `server`.

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [December 4, 2024, 9:27pm UTC](https://discourse.cmake.org/t/add-library-in-a-config-cmake-causing-problem/13122/7 "2024-12-04T21:27:07Z")

</div>

> [@retif](#):
>
> Although since your target name is `flock-server`, it will then become `flock::flock-server`, so then you’d probably want to rename it to just `server`.

The `EXPORT_NAME` target property can help with that. For example:

```cmake
set_target_properties(flock-server PROPERTIES EXPORT_NAME server)

install(EXPORT flock-targets
    FILE flock-targets.cmake
    NAMESPACE flock::
    DESTINATION "${flock-pkg}" # assuming this path is correct
)

```

The above would result in the `flock-targets.cmake` file defining the target as `flock::server`.
