On Tue, 2019-09-24 at 23:41 +0300, Avraham Shukron wrote:
> Hi!
> 
> I have a library which I want to distribute in both shared object and
> static library forms.
> Is there a modern way to do it without creating two completely
> separate library targets?
> Since I want to be a good CMake citizen I use `target_*` and
> `set_target_properties` as much as possible, and creating two
> different libraries will force me to duplicate that information about
> each one of them.
> 
> I also tries not specifying STATIC/SHARED, and then running cmake
> twice - once with BUILD_SHARED_LIBS=ON and once OFF and then
> installing to the same directory. I got my both .a and .so libraries
> installed, but I couldn't get the Config file correctly for this
> arrangement.
> 
> So - what is the community-recommended pattern to do this?

Unfortunately, the recommendation is to do exactly what you don't want
to do: create a shared target and a static target.

To make this slightly simpler, you can use functions to de-duplicate
the target_* and set_target_properties calls:

function(create_foo_target name type)
  add_library(${name} ${type} foo.c foo.h)
  set_target_properties(${name} OUTPUT_NAME foo)
endfunction()

create_foo_target(foo_shared SHARED)
create_foo_target(foo_static STATIC)

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 via email to