<div dir="ltr"><div>I think I have found a way to work round this.  Instead of trying to get a list of all the static libraries that a target will link against I will modify the various libraries so that they add their library dependencies like this:<br>
<br>    target_link_libraries(mtx_wrapper PUBLIC<br>        $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,SHARED_LIBRARY>:-Wl,-whole-archive><br>        ${MTXSIM_LIBRARIES}<br>        $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,SHARED_LIBRARY>:-Wl,-no-whole-archive><br>
        $<$<STREQUAL:$<CXX_COMPILER_ID>,GNU>:dl><br>        $<$<STREQUAL:$<CXX_COMPILER_ID>,Clang>:dl><br><br></div><div>This should mean that all the dependent libraries get wrapped inside -whole-archive/-no-whole-archive pairs when the target being linked is a shared library.  And if the target being linked is an executable then the -whole-archive/-no-whole-archive options should automatically be omitted.  Initial tests indicate that this approach will work but I have encountered a bug which slightly complicates the issue:<br>
</div><div><br><div style="margin-left:40px"><a href="http://www.cmake.org/Bug/view.php?id=15034">http://www.cmake.org/Bug/view.php?id=15034</a><br></div><br></div><div>Essentially, the -no-whole-archive option sometimes gets omitted unless prefixed by some other command.<br>
<br></div><div>--<br></div>Glenn<br><br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On 21 July 2014 19:24, Angeliki Chrysochou <span dir="ltr"><<a href="mailto:angeliki.chrysochou@gmail.com" target="_blank">angeliki.chrysochou@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Well try it and see if it works. If you set this variable in a specific CMakeLists file, it will affect the specific add_library (or add_executable) that is in that CMakeLists file. With this way you can control which libraries/executables will be linked with these flags, and this is the level of control you have. <div>

<br></div><div>Cheers!</div><span class="HOEnZb"><font color="#888888"><div>Angeliki</div><div><br></div></font></span></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Jul 21, 2014 at 7:05 PM, Glenn Coombs <span dir="ltr"><<a href="mailto:glenn.coombs@gmail.com" target="_blank">glenn.coombs@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>The problem is that I need to add both -whole-archive and -no-whole-archive options.  And I need to control exactly where they occur so that only my libraries occur inside the whole archive section.  I'd be happy to be proven wrong but I don't think setting CMAKE_EXE_LINKER_FLAGS will give me that level of control.<br>


<br>--<br></div>Glenn<br></div><div><div><div class="gmail_extra"><br><br><div class="gmail_quote">On 21 July 2014 09:57, Angeliki Chrysochou <span dir="ltr"><<a href="mailto:angeliki.chrysochou@gmail.com" target="_blank">angeliki.chrysochou@gmail.com</a>></span> wrote:<br>


<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div>Hi Glenn,<br><br></div>Adding linker flags exactly in target_link_libraries is not a very good practice in my opinion. To add specific linker flags to an executable, you can use the variable CMAKE_EXE_LINKER_FLAGS, which you can edit before calling add_executable. You could set this variable accordingly in your static and dynamic CMakeLists.txt to include the flags you wish in the following way:<br>



<br>set(CMAKE_EXE_LINKER_FLAGS "-whole-archive")<br><br></div><div>If you set this variable to include more custom linker flags which you want to preserve across libraries, then you should set it in the following way to preserve its old value:<br>



<br>set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -whole-archive")<br><br></div><div>If you want to edit linker flags for a shared library or a module target you can use these cmake variables (set them before add_library):<br>



<br>CMAKE_SHARED_LINKER_FLAGS<br>CMAKE_MODULE_LINKER_FLAGS<br></div><div><br>Be aware that variables have CMakeLists.txt file scope, so if you set
 different values in one CMakeLists.txt then they might get overwritten 
or appended (depending on the way you edit the variable).<br><br></div><div>All the best,<br>Angeliki<br></div><div><br><br><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote"><div><div>On Sat, Jul 19, 2014 at 2:39 PM, Glenn Coombs <span dir="ltr"><<a href="mailto:glenn.coombs@gmail.com" target="_blank">glenn.coombs@gmail.com</a>></span> wrote:<br>



</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div dir="ltr"><div>Don't all shout at once :-)  I'm guessing there are no easy solutions then...<br>


<br>--<br>
</div>Glenn<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On 28 June 2014 14:33, Glenn Coombs <span dir="ltr"><<a href="mailto:glenn.coombs@gmail.com" target="_blank">glenn.coombs@gmail.com</a>></span> wrote:<br>




<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div>I have a project which compiles and links into both a stand alone executable and a dynamic shared library.  The library and the executable link against the same project libraries but have different object files containing their entry points: main.o for the executable and dll_main.o for the library.  My project heirarchy looks like this (simplified a bit for brevity):<br>





<br></div>CMakeLists.txt<br></div>    mtx_source/CMakeLists.txt<br></div>    mtx_wrapper/CMakeLists.txt<br></div>    testbench/CMakeLists.txt<br><div><div><div><div><br></div><div>The top level CMakeLists.txt calls add_subdirectory on the various project specific library folders which each build a static library.  The testbench folder is the one that builds both the executable and the dynamic library.  When building the dynamic library I need to use the linker options 
-whole-archive and -no-whole-archive to force my static project libraries to be
 included into the dynamic library. The problem is how to insert the -whole-archive and -no-whole-archive options so that they select the correct set of libraries.  Currently in the testbench CMakeLists.txt file I have these lines:<br>





<br></div><div><div style="margin-left:40px">set(libs<br>    VLC<br>    mvea<br>    ${SYSTEMC_SUPPORT_LIBRARIES}<br>    ${DEVIFSLAVE_LIBRARIES}<br>    ${SYSTEMC_LIBRARIES}<br>    ${SIM_UTILS_LIBRARIES}<br>    ${HWDEBUG_LIBRARIES}<br>





)<br><br>if (NOT STUB_OUT_MTX)<br>    list(APPEND libs mtx_wrapper)<br>endif()<br><br>set(libs_dll ${libs} transif_slave)<br><br>if (UNIX)<br>    list(INSERT libs_dll 0 -Wl,-whole-archive)<br>    list(APPEND libs_dll   -Wl,-no-whole-archive)<br>





endif()<br><br>add_library ( csim_dll SHARED ${sources_dll} ${headers_dll} )<br>add_executable( testbench       ${sources}     ${headers}     )<br><br>target_link_libraries(csim_dll  ${libs_dll} ${PTHREADS_LIBRARIES} )<br>





target_link_libraries(testbench ${libs}     ${PTHREADS_LIBRARIES} )<br></div><br></div><div>which produces the following link line:<br><br><div style="margin-left:40px">/usr/bin/g++-4.7  -fPIC -m32  -m32 -m32 -fPIC -m32 -O3  -O3 -DHIDEBUG   <br>





-Wl,-Bsymbolic <br>-shared -Wl,-soname,libtopazhp.so<br>-o libtopazhp.so<br>CMakeFiles/csim_dll.dir/dll_main.cpp.o<br>CMakeFiles/csim_dll.dir/main_common.cpp.o<br>-lm -lrt -lm -lrt<br>-Wl,-whole-archive<br>../mvea/VLC/libVLC.a<br>





../mvea/libmvea.a<br>../systemCSupport/libsystemc_support.a<br>../devif_slave/libDevifSlave.a<br>../systemC/libsystemc.a<br>../sim_utils/libsim_utils.a<br>../hwdebug/libhwDebug.a<br>../mtx_wrapper/libmtx_wrapper.a<br>../transif/libtransif_slave.a<br>





-Wl,-no-whole-archive<br>-lpthread -lz <br>../systemC/libpthreads_dummy.a <br>../external_mtx/src/external_mtx-build/metag/libmetag.a <br>../external_mtx/src/external_mtx-build/metagcopro/libmetagcopro.a <br>../external_mtx/src/external_mtx-build/metac/vmetastub/libvmetastub.a <br>





../external_mtx/src/external_mtx-build/metac/insim/libinsimfpu.a <br>../external_mtx/src/external_mtx-build/mtx/insim-mtxg/libinsim-mtxg.a <br>../external_mtx/src/external_mtx-build/mtx/libmtxc.a <br>-ldl -lm -lrt -lm -lrt <br>





</div><br></div><div>The problem is that the 6 external_mtx libraries should have been included inside the -whole-archive section.  These libraries are specified in the mtx_wrapper folder with a target_link_libraries(mtx_wrapper ${METASIM_LIBRARIES}) command.  I have managed to wrap the direct library dependencies inside the -whole-archive but I need to ensure that any sub-dependencies are included as well (and any dependencies they might have recursively).  Any system dynamic libaries (-ldl -lm -lrt etc.) must appear after the -no-whole-archive option otherwise the link fails.  The mtx_wrapper library can be built in two ways and only one of them will add the extra METASIM libraries as a dependency, the other way fakes that code internally.  Adding the METASIM libraries via target_link_libraries() inside the mtx_wrapper CMakeLists.txt correctly handles that dependency when linking the standalone executable but is not working for linking the dynamic library.<br>





<br>Is there an easy way to get cmake to handle all this ?  Is there a way to get a list of all the static libraries (libXXX.a) that will be included on the link line for a target ?<span><font color="#888888"><br>
<br></font></span></div><span><font color="#888888"><div>--<br></div><div>Glenn<br>

<br></div></font></span></div></div></div></div>
</blockquote></div><br></div>
<br></div></div><div>--<br>
<br>
Powered by <a href="http://www.kitware.com" 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" 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" target="_blank">http://cmake.org/cmake/help/support.html</a><br>
CMake Consulting: <a href="http://cmake.org/cmake/help/consulting.html" target="_blank">http://cmake.org/cmake/help/consulting.html</a><br>
CMake Training Courses: <a href="http://cmake.org/cmake/help/training.html" 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" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://public.kitware.com/mailman/listinfo/cmake" target="_blank">http://public.kitware.com/mailman/listinfo/cmake</a><br></div></blockquote></div><br></div>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>