linux - Using CMake to find symbols in libraries -
i trying use cmake find if external library depends on library.
example: hdf5 library can optionally linked zlib in linux, can found with
readelf -ws /usr/local/lib/libhdf5.so | grep inflateend 54: 0000000000000000 0 func global default und inflateend
with cmake seems can found checklibraryexists
https://cmake.org/cmake/help/v3.0/module/checklibraryexists.html
in cmake script
set(cmake_required_libraries ${hdf5_library}) check_library_exists(${hdf5_library} inflateend "" need_zlib) message(${need_zlib}) if (need_zlib) message("-- zlib library needed...") else() message("-- zlib library not needed...") endif()
output not found
-- looking inflateend in /usr/local/lib/libhdf5.so - not found -- zlib library not needed...
because of did cmake version of using readelf, finds symbol
but, still know why above cmake script fails :-)
the working version is
set(have_read_symbols "no") find_program(read_symbols_prg readelf) if (${read_symbols_prg} matches "readelf") set(have_read_symbols "yes") message("-- readelf program found: " ${read_symbols_prg}) execute_process(command ${read_symbols_prg} -ws ${hdf5_library} output_file ${cmake_current_binary_dir}/symbols.txt) endif() if (${have_read_symbols} matches "yes") message("-- detecting if hdf5 library ${hdf5_library} needs zlib library...") file(strings ${cmake_current_binary_dir}/symbols.txt need_zlib regex "inflateend") if (need_zlib) message("${color_blue}-- zlib library needed...${color_reset}") else() message("-- zlib library not needed...") endif(need_zlib) endif()
that finds symbol
-- detecting if hdf5 library /usr/local/lib/libhdf5.so needs zlib library... -- zlib library needed...
ok, cmake documentation checklibraryexists() short on details on how accomplished, "check if function exists". checklibraryexists() tries link test program, , depending on success or not, output variable parameter set value 1 or empty.
in case, symbol must 1 defined in libhdf5.so using zlib library. there 1 , 1 symbol case, structure named "h5z_deflate" added library depending on #ifdef zlib case.
so, trick
check_library_exists(${hdf5_library} h5z_deflate "" need_zlib)
however windows case using visual studio error prone, because check_library_exists() detect it, visual studio must set option additional dependencies zlib library, , not requirement library build successfully. so, if option set, check_library_exists detects dependency, if not set, not.
Comments
Post a Comment