[CMake] Proper process for static & shared variants of config packages?

P F pfultz2 at yahoo.com
Thu Sep 7 00:07:22 EDT 2017


> On Sep 1, 2017, at 10:39 AM, Robert Dailey <rcdailey.lists at gmail.com> wrote:
> 
> Suppose I have a library target and I setup a config package for it
> and install target exports for it. What is the process for supporting
> installation of the shared library and static library variants (maybe
> the same answer applies to debug and release variants too)?
> 
> Should you create 1 target and rely on BUILD_SHARED_LIBS, which means
> generating two binary directories, building and installing once in
> each?

This is the best approach as it leaves the decision of building shared or static to the clients.

> Will this overwrite existing target.cmake and config.cmake files
> in a negative way?

You could install each variant of shared and static to separate install directories. For debug and release you can install together, as cmake generates a target-<configuration>.cmake for each configuration. I suppose you could embed some logic in the config.cmake file to pick a different export file for shared or static as well.

> 
> Or should there be 2 library targets (something like foo_shared and
> foo_static) and build & install once?

No, this is rather problematic:

1) If your dependencies are built for just one variant as well, then one of those targets could fail.

2) A downstream library may only want to create 1 target, and now it has to add extra logic to decide if it should choose the shared or static target, which is cumbersome.




More information about the CMake mailing list