[cmake-developers] Exporting a library shared and static

Gereon Kremer gereon.kremer at cs.rwth-aachen.de
Tue Sep 16 10:52:41 EDT 2014


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi all,

I have a project that consists of a library and an application that are
developed as two separated projects (different repos, separate cmake
setups, etc.). Let's call them "lib" and "app".
The app exports itself to ~/.cmake/ and creates a appTargets.cmake, the
app simply does find_package(lib).
For several reasons, we must be able to build the app dynamically and
statically.

Our current approach is based on a flag "STATICLIB_SWITCH" that exists
in the lib and in the app.
Based on it's value, we build the library as .a or .so and link the app
statically or dynamically.
However, there are a few drawbacks: The value of the switch has to be
consistent for the lib and the app and we didn't quite manage to
correctly search for all libs: The linker command line is cluttered with
lots of -rdynamic etc, which also occasionally breaks down and is very
hard to debug when it does.

So I spent some time on this and it seems that this is meant to be done
differently. My new setup looks like this:
The lib has two targets, for a shared object and for a static object,
that are always built and exported.
The app includes whatever it needs.
This would remove the switch from the lib, which would be very nice.
However, it seems that the app always includes the "shared target", no
matter what I do.
Also, I have not been able to provide the target with the list of
libraries necessary: I simply collect all libraries in a variable that
are found by other find_package calls, but find_package will only find
either the shared or the static versions...

So basically my question is: How is this supposed to be done? (I somehow
get the feeling, that both attempts are wrong :-) )
How can I use a library in another application and switch (fairly)
easily between static and dynamic linking?

Thanks for any hints on this!
Gereon
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQEcBAEBAgAGBQJUGE65AAoJEIQ2nMX673HfLL0H/RcCrNRKdLVQaaIfJnICKCiw
5RaE6t8PXxCjD+XPBgQOmdDVOKXAy/f/t7gV1T6yRDvvbgbUyQnTpRoWUL0Qjlos
J+qob54Lcm90DTglNkpnImbfdBRv3XPDS34AGA20kgkmSDVsTdhg1fjDf5Cb10f7
UlGySIQqiIWSuygiq5uawswxqQuh6VuL98/vY+vxCkjNbLnWzuvCAT5x692qaFH3
M7JI4P/SWii637z/7sMh9e+mHue6MBynrcff2PUhDNCyIiG9MiQbZfzsvcQoKYSO
jDZLTvSvmy8FUPXPH+15Z8MyfjqjksErLX4UbOBFeEQ5FJwtA+G9TRYHzlDS9vc=
=AfDK
-----END PGP SIGNATURE-----




More information about the cmake-developers mailing list