[CMake] Yet another static libraries order question
Pritchett-Sheats, Lori
lpritch at lanl.gov
Tue May 28 09:24:35 EDT 2013
I ran into a similar problem when I wrote a FindHDF5.cmake module for a project. My solution was to create imported targets and define the library dependencies with the LINK_INTERFACE_LIBRARIES property.
Example:
Through find_library I've located libhdf.a Location name is saved in the variable HDF5_C_LIBRARY
add_library(hdf5 IMPORTED)
set_target_properties(hdf5 PROPERTIES
IMPORTED_LOCATION ${HDF5_C_LIBRARY}
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
)
And supposed I have the HDF5 High level library saved in HDF5_HL_LIBRARY
add_library(hdf5_hl IMPORTED)
set_target_properties(hdf5_hl PROPERTIES
IMPORTED_LOCATION ${HDF5_HL_LIBRARY}
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
IMPORTED_LINK_INTERFACE_LIBRARIES hdf5
)
And the Fortran library is HDF5_Fortran_LIBRARY
add_library(hdf5_fortran IMPORTED)
set_target_properties(hdf5_fortran PROPERTIES
IMPORTED_LOCATION ${HDF5_Fortran_LIBRARY}
IMPORTED_LINK_INTERFACE_LANGUAGES "Fortran;C"
IMPORTED_LINK_INTERFACE_LIBRARIES hdf5
)
etc...
Now in a calling CMakeLists.txt file, if some piece of code needs the Fortran HDF5 library, you would have
find_package(HDF5)
add_library(myflib ${SOURCE_FILES})
target_link_libraries(myflib hdf5_fortran)
and CMake now "knows" about the dependency between hdf5_fortran and hdf5 and should take of the link order for you.
Lori A. Pritchett-Sheats
Los Alamos National Laboratory
CCS-2, Computational Physics
505-665-6675
________________________________________
From: cmake-bounces at cmake.org [cmake-bounces at cmake.org] on behalf of Julien Bigot [julien at julien-bigot.fr]
Sent: Saturday, May 25, 2013 3:59 PM
To: cmake at cmake.org
Subject: [CMake] Yet another static libraries order question
Hi,
I have yet another static library order question when using
target_link_libraries
In my case, I've developed a new FindHDF5.cmake that calls the hdf5 compiler
wrapper and sets the usual variables
HDF5_<lang>_COMPILE_FLAGS, HDF5_<lang>_INCLUDE_PATH, HDF5_<lang>_LINK_FLAGS
and HDF5_<lang>_LIBRARIES
In my application I'm using both C and Fortran so I'm adding the libraries
with:
target_link_libraries(myapp ${HDF5_C_LIBRARIES} ${HDF5_Fortran_LIBRARIES})
I'm using this on a bluegene/q computer where the found libraries are
HDF5_Fortran_LIBRARIES:
/usr/local/hdf5/v1.8.10p1_serial/lib/libhdf5hl_fortran.a
/usr/local/hdf5/v1.8.10p1_serial/lib/libhdf5_hl.a
/usr/local/hdf5/v1.8.10p1_serial/lib/libhdf5_fortran.a
/usr/local/hdf5/v1.8.10p1_serial/lib/libhdf5.a
/usr/local/szip/lib/libsz.a;/usr/local/zlib/lib/libz.a
/bgsys/drivers/toolchain/V1R2M0/gnu-linux/powerpc64-bgq-linux/lib/librt.a
/bgsys/drivers/toolchain/V1R2M0/gnu-linux/powerpc64-bgq-linux/lib/libm.a
HDF5_C_LIBRARIES:
/usr/local/hdf5/v1.8.10p1_serial/lib/libhdf5_hl.a
/usr/local/hdf5/v1.8.10p1_serial/lib/libhdf5.a
/usr/local/szip/lib/libsz.a;/usr/local/zlib/lib/libz.a
/bgsys/drivers/toolchain/V1R2M0/gnu-linux/powerpc64-bgq-linux/lib/librt.a
/bgsys/drivers/toolchain/V1R2M0/gnu-linux/powerpc64-bgq-linux/lib/libm.a
the order match the dependencies:
hdf5hl_fortran -> hdf5_hl + hdf5_fortran + hdf5
hdf5_hl -> hdf5
hdf5_fortran -> hdf5
However cmake removes duplicates from the library list in
target_link_libraries ... unfortunately, it leaves the first duplicate and
rmoves the following ones which results in the order
"hdf5_hl hdf5 hdf5hl_fortran hdf5_fortran"
That doesn't work due to the dependencies
hdf5hl_fortran -> hdf5_hl + hdf5
hdf5_fortran -> hdf5
I don't get what I'm doing wrong here (apart from trying to use both static
libs and cmake :/ ) ...
For now I've worked around this problem by putting the C libs after the
fortran ones in the call to target_link_libraries but I don't want to
distribute my module with this limitation ...
Could someone help me there ?
Regards,
--
Julien Bigot, Ph.D. -- http://work.julien-bigot.fr
Maison de la Simulation USR 3441 - bât. 565 - CEA Saclay
91191 Gif-sur-Yvette CEDEX - FRANCE
phone: (+33) 1 69 08 01 75 -- xmpp: jbigot at jabber.org
More information about the CMake
mailing list