Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cmake test : was a library compiled/linked against libc++ or libstd++?

I am using cmake to manage my project that uses a third party library.

This library could have been compiled/linked against libc++ or libstd++ (Depending on the version).

I know how to tell cmake to compile/link my project against libc++ or libstdc++, but I don't know how to check if the library I am using was compiled/linked against libc++ or libstd++. Is there any cmake command to check that?

like image 634
Issam T. Avatar asked May 07 '15 08:05

Issam T.


2 Answers

For shared libraries you can use the GetPrerequisites standard module to test if the library depends on libstc++ or libc++.

For example, the following code test if boost's program_options library has been compiled against libstc++ or libc++:

set (_library "/usr/local/lib/libboost_program_options.dylib")
set (_prequesites "")
set (_exclude_system FALSE)
set (_recurse FALSE)
set (_exePath "")
set (_searchDirs "")
get_prerequisites(${_library} _prequesites ${_exclude_system} ${_recurse} "${_exePath}" "${_searchDirs}")
if (_prequesites MATCHES "/libstdc\\+\\+")
    message("using libstc++")
elseif (_prequesites MATCHES "/libc\\+\\+")
    message("using libc++")
else()
    message("using neither libstc++ nor libc++")
endif()

For static libraries you probably have to resort to running nm on the library file to determine external symbols and then search for characteristic strings like __gnu_ in the output.

like image 145
sakra Avatar answered Sep 27 '22 20:09

sakra


Do you have an error if you link to the wrong version ? If it's the case, you can use try_compile from CMake. Example of use :

try_compile(
  TRY_COMPILE_SUCCESS
  ${CMAKE_BINARY_DIR}/tmpTryDir
  ${CMAKE_MODULES_DIR}/SourceFile.cpp
  CMAKE_FLAGS
    "-DINCLUDE_DIRECTORIES=${TRY_INCLUDE_DIRS}"
    "-DLINK_DIRECTORIES=${TRY_LIBRARY_DIRS}"
    "-DLINK_LIBRARIES=${TRY_LIBRARIES}"
  COMPILE_DEFINITIONS
    "-DCOMPILER_OPTION"
)

And then, the CMake variable TRY_COMPILE_SUCCESS contains TRUE or FALSE depending of the compilation success.

like image 44
Caduchon Avatar answered Sep 27 '22 21:09

Caduchon