[CMake] Proper way to export a library

Matthew Woehlke matthew.woehlke at kitware.com
Wed Oct 30 16:10:31 EDT 2013


On 2013-10-30 13:56, Cyrille Faucheux wrote:
> I'm trying to learn how to properly import/export librairies with CMake.
>
> I think I've read somewhere that the proper way to export libraries is
> through the module mode (as described in [1]), but:
> a) I can't find back where I've read that,
> b) Lots of libraries are still using the module mode (Find*.cmake).
>
> Can you confirm me which mode I should choose?

It's definitely preferred these days to work with exported/imported 
targets where possible. Unlike find modules, imported targets let you 
link to target names in consuming projects. Also, now that target usage 
requirements have arrived, you can have implicit compile flags, include 
directories, and interface link libraries for imported targets. All of 
this makes it much easier to use external libraries and lessens the 
distinction between an imported and local target.

Additionally, this happens in one place. Find modules tend to end up 
copied about to different projects until/unless they wind up in CMake 
proper, and even then can linger on, resulting in multiple versions in 
varying states of bitrot. They can even be written multiple times by 
different people in unrelated projects. By providing a package config 
file (even one that just sets find module style variables) you prevent 
this proliferation and duplication of work.

The flip side (and why you have a lot of find modules still) is that the 
onus to provide a package config file is on the upstream. This means 
that upstream must either build with CMake in the first place, and have 
the necessary logic in the CMake build instructions to generate a 
package config file (ideally with exported targets), or else be friendly 
enough to CMake to generate 'by hand' such a file despite using some 
other build system. It should go without saying that, especially in the 
latter case, politics can become involved here.

-- 
Matthew



More information about the CMake mailing list