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

2

5

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?

Visit answered 7/5, 2015 at 8:23 Comment(0)
J
5

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.

Jacobah answered 11/5, 2015 at 19:30 Comment(0)
L
2

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.

Lippert answered 7/5, 2015 at 8:51 Comment(2)
I may compile against libstd++ successfully then have a link error when it discovers that the third party lib was linked against libc++, hence the need of recompiling again the whole project against libc++. Your solution actually automatize this process. Nevertheless, i hope for a better solution. thanks anyway.Visit
This looks like an elegant solution!Mourner

© 2022 - 2024 — McMap. All rights reserved.