[CMake] How to get a list of all the static libraries that a target will link against ?

Glenn Coombs glenn.coombs at gmail.com
Sat Jun 28 09:33:50 EDT 2014


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):

CMakeLists.txt
    mtx_source/CMakeLists.txt
    mtx_wrapper/CMakeLists.txt
    testbench/CMakeLists.txt

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:

set(libs
    VLC
    mvea
    ${SYSTEMC_SUPPORT_LIBRARIES}
    ${DEVIFSLAVE_LIBRARIES}
    ${SYSTEMC_LIBRARIES}
    ${SIM_UTILS_LIBRARIES}
    ${HWDEBUG_LIBRARIES}
)

if (NOT STUB_OUT_MTX)
    list(APPEND libs mtx_wrapper)
endif()

set(libs_dll ${libs} transif_slave)

if (UNIX)
    list(INSERT libs_dll 0 -Wl,-whole-archive)
    list(APPEND libs_dll   -Wl,-no-whole-archive)
endif()

add_library ( csim_dll SHARED ${sources_dll} ${headers_dll} )
add_executable( testbench       ${sources}     ${headers}     )

target_link_libraries(csim_dll  ${libs_dll} ${PTHREADS_LIBRARIES} )
target_link_libraries(testbench ${libs}     ${PTHREADS_LIBRARIES} )

which produces the following link line:

/usr/bin/g++-4.7  -fPIC -m32  -m32 -m32 -fPIC -m32 -O3  -O3 -DHIDEBUG
-Wl,-Bsymbolic
-shared -Wl,-soname,libtopazhp.so
-o libtopazhp.so
CMakeFiles/csim_dll.dir/dll_main.cpp.o
CMakeFiles/csim_dll.dir/main_common.cpp.o
-lm -lrt -lm -lrt
-Wl,-whole-archive
../mvea/VLC/libVLC.a
../mvea/libmvea.a
../systemCSupport/libsystemc_support.a
../devif_slave/libDevifSlave.a
../systemC/libsystemc.a
../sim_utils/libsim_utils.a
../hwdebug/libhwDebug.a
../mtx_wrapper/libmtx_wrapper.a
../transif/libtransif_slave.a
-Wl,-no-whole-archive
-lpthread -lz
../systemC/libpthreads_dummy.a
../external_mtx/src/external_mtx-build/metag/libmetag.a
../external_mtx/src/external_mtx-build/metagcopro/libmetagcopro.a
../external_mtx/src/external_mtx-build/metac/vmetastub/libvmetastub.a
../external_mtx/src/external_mtx-build/metac/insim/libinsimfpu.a
../external_mtx/src/external_mtx-build/mtx/insim-mtxg/libinsim-mtxg.a
../external_mtx/src/external_mtx-build/mtx/libmtxc.a
-ldl -lm -lrt -lm -lrt

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.

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 ?

--
Glenn
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20140628/4a8aa62f/attachment.html>


More information about the CMake mailing list