<div dir="ltr"><div dir="ltr">Here is an example where two libraries are created from object files that have only been compiled once:<div><br></div><div>ADD_LIBRARY(symdiff_objects OBJECT ${CXX_SRCS} ${MC_SRCS})<br>set_property(TARGET symdiff_objects PROPERTY POSITION_INDEPENDENT_CODE ON)<br>ADD_LIBRARY(symdiff_dynamic STATIC $<TARGET_OBJECTS:symdiff_objects>)<br>ADD_LIBRARY(symdiff SHARED $<TARGET_OBJECTS:symdiff_objects>)<div></div></div><div><br></div><div>The "symdiff_dynamic" library is a static archive that can then be linked into another shared library or an executable. The "symdiff" library is a standalone object.  The POSITION_INDEPENDENT_CODE property is required for the linux platform, but not for macOS or Windows.  If the original poster is comfortable with having a PIC static library, this is an approach they can take.</div><div><br></div><div>Regards,</div><div><br></div><div>Juan</div><div><br></div></div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Sep 25, 2019 at 8:43 AM Kyle Edwards via CMake <<a href="mailto:cmake@cmake.org">cmake@cmake.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Tue, 2019-09-24 at 23:41 +0300, Avraham Shukron wrote:<br>
> Hi!<br>
> <br>
> I have a library which I want to distribute in both shared object and<br>
> static library forms.<br>
> Is there a modern way to do it without creating two completely<br>
> separate library targets?<br>
> Since I want to be a good CMake citizen I use `target_*` and<br>
> `set_target_properties` as much as possible, and creating two<br>
> different libraries will force me to duplicate that information about<br>
> each one of them.<br>
> <br>
> I also tries not specifying STATIC/SHARED, and then running cmake<br>
> twice - once with BUILD_SHARED_LIBS=ON and once OFF and then<br>
> installing to the same directory. I got my both .a and .so libraries<br>
> installed, but I couldn't get the Config file correctly for this<br>
> arrangement.<br>
> <br>
> So - what is the community-recommended pattern to do this?<br>
<br>
Unfortunately, the recommendation is to do exactly what you don't want<br>
to do: create a shared target and a static target.<br>
<br>
To make this slightly simpler, you can use functions to de-duplicate<br>
the target_* and set_target_properties calls:<br>
<br>
function(create_foo_target name type)<br>
  add_library(${name} ${type} foo.c foo.h)<br>
  set_target_properties(${name} OUTPUT_NAME foo)<br>
endfunction()<br>
<br>
create_foo_target(foo_shared SHARED)<br>
create_foo_target(foo_static STATIC)<br>
<br>
Kyle<br>
-- <br>
<br>
Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
<br>
Please keep messages on-topic and check the CMake FAQ at: <a href="http://www.cmake.org/Wiki/CMake_FAQ" rel="noreferrer" target="_blank">http://www.cmake.org/Wiki/CMake_FAQ</a><br>
<br>
Kitware offers various services to support the CMake community. For more information on each offering, please visit:<br>
<br>
CMake Support: <a href="http://cmake.org/cmake/help/support.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/support.html</a><br>
CMake Consulting: <a href="http://cmake.org/cmake/help/consulting.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/consulting.html</a><br>
CMake Training Courses: <a href="http://cmake.org/cmake/help/training.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/training.html</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="https://cmake.org/mailman/listinfo/cmake" rel="noreferrer" target="_blank">https://cmake.org/mailman/listinfo/cmake</a><br>
</blockquote></div></div>