Allow export of IMPORTED libraries?

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 :slight_smile:

1 Like

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 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.