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.

2 Likes

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

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…

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

1 Like