[CMake] FetchContent or Export Targets

Craig Scott craig.scott at crascit.com
Sun Mar 24 03:40:44 EDT 2019


On Sun, Mar 24, 2019 at 1:43 PM Jason Beach <jason.m.beach at gmail.com> wrote:

> Hi,
>   As I've been learning CMake, it seems the best practice for a library is
> for it to export the targets it creates. There is also the relatively new
> command FetchContent. It seems that at least in part the FetchContent
> command obviates the need for exporting targets.
>
> I can see that for larger more permanent libraries exporting targets is
> still preferred so the library can be installed once and used by multiple
> projects on a developers workstation without having to install it on each
> project.
>
> At work we're a bunch of research engineers that happen to write a lot of
> software-- exporting targets still seems cumbersome and it seems not
> exporting targets and then just have users use FetchContent to pull in a
> library is not a bad idea.
>
> Are there other reasons /advantages of exporting targets?
>

Exporting targets is a way to allow other projects to easily consume yours
via find_package(). If you are 100% confident that no-one is ever going to
use your project that way and will only ever build it from source directly
(whether that be through FetchContent or some other method), then there's
no benefit to exporting targets. As soon as there's a possibility that
someone might want to build against an installed version of your project
though, having the exported targets available makes for a significantly
more convenient and more robust experience. It really comes down to whether
in your case you can truly say no-one will ever want to use find_package()
to bring your projects into theirs.


-- 
Craig Scott
Melbourne, Australia
https://crascit.com

Get the hand-book for every CMake user: Professional CMake: A Practical
Guide <https://crascit.com/professional-cmake/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20190324/e96354e6/attachment.html>


More information about the CMake mailing list