<div dir="ltr"><div dir="ltr"><div>Thank you all for the help. <br></div><div><br></div><div>I finally use a way quite similar to David's first approach. I first generate all sub-projects into object libraries using add_library(lib1 OBJECT SOURCES). Then in the final library, I use add_library(single_static_lib STATIC SOURCES) and target_link_libraries( single_static_lib lib1 lib2 ...). Note that I didn't use " $<TARGET_OBJECTS:lib1>" in the final "add_library" and also didn't use "PUBLIC" keyword in the final "target_link_libraries". It works on CMake v3.12.2 and gives me a single static lib which combines all the objs I want. <br></div><div><br></div><div>But currently I still have problems of further combining third party static libraries into the final generated static single_static_lib. Can anybody provide a solution for this?</div><div><br></div><div>Thank you very much.</div><div><br></div><div>Ke<br></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Fri, Sep 21, 2018 at 6:15 AM Deniz Bahadir <<a href="mailto:dbahadir@benocs.com">dbahadir@benocs.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Am 21.09.2018 um 09:33 schrieb David Jobet:<br>
> Hello,<br>
> <br>
> I had a similar issue lately and wanted to "pack" several static libs<br>
> into a dynamic one. (Not even talking about an INTERFACE lib since I<br>
> really wanted that .so)<br>
> I made it work with 3 different solutions, none of them being "clean"<br>
> from my point of view.<br>
> <br>
> 1- OBJECT libs on sub projects : add_library(lib1 OBJECT SOURCES) and<br>
> for the single static lib : add_library(single_static_lib STATIC<br>
> $<TARGET_OBJECTS:lib1> ...)<br>
> Problem I faced : since OBJECT libs do not support<br>
> target_link_libraries(), I had to remove the existing one and move<br>
> them instead to the target_include_directories() using generators.<br>
<br>
This is no longer true. Since CMake 3.12 `target_link_libraries` fully <br>
supports OBJECT libraries. You just need to pay attention to the special <br>
case of linking an OBJECT library with keyword "PUBLIC". (Object-files <br>
are always private and inherited object-files are therefore never <br>
further inherited. See: <br>
<a href="https://cmake.org/cmake/help/v3.12/command/target_link_libraries.html#linking-object-libraries" rel="noreferrer" target="_blank">https://cmake.org/cmake/help/v3.12/command/target_link_libraries.html#linking-object-libraries</a>)<br>
<br>
> e.g : target_include_directories(lib1 PUBLIC<br>
> $<TARGET_PROPERTY:another_lib,INCLUDE_DIRECTORIES>)<br>
> Because I had a dependency to a protobuf generated lib, I also had to<br>
> add manual add_dependencies to respect proper build order.<br>
> Not clean at all<br>
> <br>
> 2- add_library(mysharedlib STATIC CMakeLists.txt)<br>
> target_linked_libraries(mysharedlib PUBLIC lib1 ...)<br>
> Maybe the cleanest way I found.<br>
> The trick with CMakeLists.txt is that add_library needs at least one<br>
> source file. You can put any kind of files you want. CMakeLists.txt is<br>
> not compilable, so no extra compilation step, no need for dummy empty<br>
> source file and add_library is happy.<br>
> It did not work in my case because of problems related to how our .so<br>
> are used/generated. (problems at runtime with duplicated symbols in<br>
> protobufs)<br>
> <br>
> 3- a variation around 1<br>
> instead of defining OBJECT libs, define a variable holding all the<br>
> sources for lib1, another for lib2, ...<br>
> then just do add_library(mysharedlib STATIC ${SOURCES_FOR_lib1}<br>
> ${SOURCES_FOR_lib2})<br>
> It works a little bit like 1) but does not have any of its problems<br>
> (target_link, add_dependencies, generators, ...)<br>
> It has new problems of its own though : if your libs live in different<br>
> subfolders, the variables might not be visible from your<br>
> add_library(mysharedlib...) call.<br>
> To work around that, you can use PARENT_SCOPE (not sure if that works<br>
> accross several levels), or includes (defines those variables into<br>
> separate files living in each subfolders and include them in the<br>
> parent CMakeLists).<br>
> <br>
> Hope this helps (and I'd be happy to know of other ways)<br>
> <br>
> David<br>
> On Thu, Sep 20, 2018 at 5:45 PM Ke Gao <<a href="mailto:ke.gao.ut@gmail.com" target="_blank">ke.gao.ut@gmail.com</a>> wrote:<br>
>><br>
>> Hi,<br>
>><br>
>> I have a project which includes many sub-projects. Each sub-project generates a static library. In the main project, I want to combine the generated objs, generated static libraries from other sub-projects, and some other third party static libraries together into a single static library. Is there an elegant way to do this, or maybe an existing function?<br>
>><br>
>> Thank you very much.<br>
>><br>
>> --<br>
>> ..............................................................................................................................................<br>
>> Ke Gao<br>
<br>
<br>
Hope that information was of value,<br>
Deniz<br>
<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><br clear="all"><br>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div dir="ltr"><div><font color="#000000" face="arial,helvetica,sans-serif" size="1">..............................................................................................................................................</font></div><font color="#3366ff" face="tahoma,sans-serif"><div><font color="#000000" face="arial black,sans-serif" style="background-color:rgb(255,255,255)">Ke Gao</font></div></font></div></div></div></div></div></div>