# Allow export of IMPORTED libraries?

**URL:** https://discourse.cmake.org/t/allow-export-of-imported-libraries/13088
**Category:** Development
**Created:** [November 28, 2024, 3:20pm UTC](https://discourse.cmake.org/t/allow-export-of-imported-libraries/13088 "2024-11-28T15:20:51Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![johe](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/e9c0ed/32.png) [@johe](https://discourse.cmake.org/u/johe)
#### Post date: [November 28, 2024, 3:20pm UTC](https://discourse.cmake.org/t/allow-export-of-imported-libraries/13088/1 "2024-11-28T15:20:51Z")

</div>

I want to export targets of a prebuild library. I.e. I only have .dll and .lib files and want to create a target from them and make it available via a find\_package() call.

From what I understand the CMakeLists.txt should create a SHARED IMPORTED library to create the target. Exporting IMPORTED targets is not allowed though. Is this just not implemented or is there a good reason this is not allowed. If there is a reason, do you have a suggestion for another approach?

I would be happy to contribute to enable exporting IMPORTED targets in any way. I just wanted to check if there is a chance to get this into CMake before spending time on this 🙂

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [November 29, 2024, 2:29pm UTC](https://discourse.cmake.org/t/allow-export-of-imported-libraries/13088/2 "2024-11-29T14:29:29Z")

</div>

This is by design. In general imported targets don’t contain enough information to re-export them. This is because `install(EXPORT)` produces relocatable exports of the project’s targets. The whole install tree can be deployed to another directory or even another machine, and still work correctly. When the targets file generated by `install(EXPORT)` is loaded by a dependent project, it computes artifact locations relative to itself and generates imported targets for the consumer. It can do this because the install tree layout is known relative to its prefix. This doesn’t work for third-party imports because they come from an arbitrary location on the system and their location after deployment is not known. Maybe the files will be part of the package. Maybe they need to be found again on the destination system with new `find_*` calls. `install(EXPORT)` doesn’t know.

If you import a third-party library from somewhere else, then so can your dependents. Whatever code you use to find and import them can go in your project’s [package configuration file](https://cmake.org/cmake/help/v3.31/manual/cmake-packages.7.html#creating-packages) and it will run in dependents to import them there. Or, the code can be different if it’s meant to be part of a package that works differently after distribution: it just has to provide the same imported targets.

---

<div class="post-metadata">

### Author: ![johe](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/e9c0ed/32.png) [@johe](https://discourse.cmake.org/u/johe)
#### Post date: [December 2, 2024, 10:42am UTC](https://discourse.cmake.org/t/allow-export-of-imported-libraries/13088/3 "2024-12-02T10:42:14Z")

</div>

Thank you for the detailed and quick answer. I proceeded as suggested and everything works perfectly.

---

<div class="post-metadata">

### Author: ![mblanchard](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ee59a6/32.png) [@mblanchard](https://discourse.cmake.org/u/mblanchard)
#### Post date: [December 10, 2024, 3:24pm UTC](https://discourse.cmake.org/t/allow-export-of-imported-libraries/13088/4 "2024-12-10T15:24:47Z")

</div>

> [@brad.king](#):
>
> The whole install tree can be deployed to another directory or even another machine, and still work correctly. When the targets file generated by `install(EXPORT)` is loaded by a dependent project, it computes artifact locations relative to itself and generates imported targets for the consumer. It can do this because the install tree layout is known relative to its prefix. This doesn’t work for third-party imports because they come from an arbitrary location on the system and their location after deployment is not known. Maybe the files will be part of the package. Maybe they need to be found again on the destination system with new `find_*` calls. `install(EXPORT)` doesn’t know.

What about `IMPORTED` targets exported from the build-tree with `export(TARGETS...)`? Is that also not allowed by design? I haven’t been quite able to do it, but not sure if not possible or just a problem with my project…

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [December 10, 2024, 4:51pm UTC](https://discourse.cmake.org/t/allow-export-of-imported-libraries/13088/5 "2024-12-10T16:51:18Z")

</div>

@mblanchard that case is also not allowed. Regardless of where the imported target came from, it is still something distributed independently of the current project. The advice in my previous post’s second paragraph is equally valid.
