Shared, static and header-only versions of library side by side.

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Shared, static and header-only versions of library side by side.

Dmitry Igrishin
Hello,

The libraries I developing can be used as a shared libraries, static
libraries or header-only libraries.
I want to let the users to:
  - install all three variants side by side;
  - choice what variant of library to use for linkage.
I see 2 options here:
  1. use different config files. For example, foo-config.cmake
(corresponds to shared library "foo"), foo_static-config.cmake
(corresponds to static library "foo"), foo_interface-config.cmake
(corresponds to header-only library "foo"). In this case users can
consume appropriate variant of "foo" by using find_package(foo CONFIGS
<config>) and then target_link_libraries(bar foo) or
target_link_libraries(bar foo_static) or target_link_libraries(bar
INTERFACE foo_interface);
  2. use different packages. For example, "foo", "foo_static",
"for_interface". In this case users can consume libraries by using
find_package(foo) or find_package(foo_static) or
find_package(foo_interface) and then target_link_libraries(bar foo) or
target_link_libraries(bar INTERFACE foo).

What option is preferable? Or is there are any other options available?
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Shared, static and header-only versions of library side by side.

Kyle Edwards
On Thu, 2019-07-04 at 00:25 +0300, Dmitry Igrishin wrote:

> Hello,
>
> The libraries I developing can be used as a shared libraries, static
> libraries or header-only libraries.
> I want to let the users to:
>   - install all three variants side by side;
>   - choice what variant of library to use for linkage.
> I see 2 options here:
>   1. use different config files. For example, foo-config.cmake
> (corresponds to shared library "foo"), foo_static-config.cmake
> (corresponds to static library "foo"), foo_interface-config.cmake
> (corresponds to header-only library "foo"). In this case users can
> consume appropriate variant of "foo" by using find_package(foo
> CONFIGS
> <config>) and then target_link_libraries(bar foo) or
> target_link_libraries(bar foo_static) or target_link_libraries(bar
> INTERFACE foo_interface);
>   2. use different packages. For example, "foo", "foo_static",
> "for_interface". In this case users can consume libraries by using
> find_package(foo) or find_package(foo_static) or
> find_package(foo_interface) and then target_link_libraries(bar foo)
> or
> target_link_libraries(bar INTERFACE foo).
>
> What option is preferable? Or is there are any other options
> available?

There is another option: create different targets, each with the same
output name. For example, you would find_package(foo), which would
yield targets foo_shared, foo_static, and foo_interface. You could then
use target_link_libraries() for each of these.

Keep in mind that they don't have to have foo_shared and foo_static in
their filenames. You can set the OUTPUT_NAME property on each target:

https://cmake.org/cmake/help/v3.15/prop_tgt/OUTPUT_NAME.html

This would allow them to still be called libfoo.a and libfoo.so.

Kyle
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Shared, static and header-only versions of library side by side.

Dmitry Igrishin
пн, 8 июл. 2019 г. в 17:00, Kyle Edwards <[hidden email]>:

>
> On Thu, 2019-07-04 at 00:25 +0300, Dmitry Igrishin wrote:
> > Hello,
> >
> > The libraries I developing can be used as a shared libraries, static
> > libraries or header-only libraries.
> > I want to let the users to:
> >   - install all three variants side by side;
> >   - choice what variant of library to use for linkage.
> > I see 2 options here:
> >   1. use different config files. For example, foo-config.cmake
> > (corresponds to shared library "foo"), foo_static-config.cmake
> > (corresponds to static library "foo"), foo_interface-config.cmake
> > (corresponds to header-only library "foo"). In this case users can
> > consume appropriate variant of "foo" by using find_package(foo
> > CONFIGS
> > <config>) and then target_link_libraries(bar foo) or
> > target_link_libraries(bar foo_static) or target_link_libraries(bar
> > INTERFACE foo_interface);
> >   2. use different packages. For example, "foo", "foo_static",
> > "for_interface". In this case users can consume libraries by using
> > find_package(foo) or find_package(foo_static) or
> > find_package(foo_interface) and then target_link_libraries(bar foo)
> > or
> > target_link_libraries(bar INTERFACE foo).
> >
> > What option is preferable? Or is there are any other options
> > available?
>
> There is another option: create different targets, each with the same
> output name. For example, you would find_package(foo), which would
> yield targets foo_shared, foo_static, and foo_interface. You could then
> use target_link_libraries() for each of these.
Thanks for the point!

Another option is to use multiple targets, components and aliasing.
For each target an alias can be created:

add_library(bar_shared ALIAS foo::bar)
add_library(baz_interface ALIAS foo::baz)

To use library we run find_package(foo COMPONENTS bar_shared baz_interface).
In foo-config.cmake the corresponding configs for bar_shared and
baz_interface included.
And then users can use "bar" and "baz" with target_link_libraries():

target_link_libraries(app foo::bar foo::baz) # using shared foo::bar
and header-only foo::baz

Thoughts?

> Keep in mind that they don't have to have foo_shared and foo_static in
> their filenames. You can set the OUTPUT_NAME property on each target:
>
> https://cmake.org/cmake/help/v3.15/prop_tgt/OUTPUT_NAME.html
>
> This would allow them to still be called libfoo.a and libfoo.so.
Unfortunately, on Windows shared library foo represented by foo.lib
and foo.dll that conflicts with static library represented by foo.lib
:-(
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake