<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 ?<br><br></div><div>--<br></div><div>Glenn<br>
<br></div></div></div></div></div>