0

I want to check if all my dependencies are compiled using libc++ or not. If that's not the case, then return a warning or an error. Some of my dependencies are shared libraries and some are static. For a shared library I currently do like this

if(MYLIB_FOUND)
    set (MYLIB_LIB "${MYLIB_LIBDIR}/lib${MYLIB_LIBRARIES}.so")
    set(MYLIB_FOUND TRUE)

    # Check if library has been compiled with -stdlib=libc++
    if(${CMAKE_VERSION} VERSION_LESS "3.16.0") 
        include(GetPrerequisites)
        GET_PREREQUISITES(${MYLIB_LIB} PREREQUISITES FALSE FALSE "" "")
        if (PREREQUISITES MATCHES "/libc\\+\\+")
            if(NOT USE_LIBCXX)
                message(WARNING "MyLib compiled using libc++ but this project does not use this flag")
                set(MYLIB_FOUND FALSE)
            endif()
        else()
            if(USE_LIBCXX)
                message(WARNING "MyLib not compiled using libc++ but this project requires this flag")
                set(MYLIB_FOUND FALSE)
            endif()
        endif()
    else()
        file(GET_RUNTIME_DEPENDENCIES RESOLVED_DEPENDENCIES_VAR MYLIB_DEPENDENCIES LIBRARIES ${MYLIB_LIB})
        if (MYLIB_DEPENDENCIES MATCHES "/libc\\+\\+")
            if(NOT USE_LIBCXX)
                message(WARNING "MyLib compiled using libc++ but this project does not use this flag")
                set(JMYLIB_FOUND FALSE)
            endif()
        else()
            if(USE_LIBCXX)
                message(WARNING "MyLib not compiled using libc++ but this project requires this flag")
                set(MYLIB_FOUND FALSE)
            endif()
        endif()
    endif()
else()
    set(MYLIB_FOUND FALSE)
endif()

How can I do the same for a static library? (Mostly CMake 3.8 and onward)

afvmil
  • 362
  • 3
  • 11

1 Answers1

0

There is no need to check this for static libraries. Static libraries are just archives with object files and they have no runtime dependencies. They are linked directly to your final binary file.

You should read more about how linker works:

If you compile your final binary without libc++ and do not have any "undefined reference" error - everything is fine. Otherwise if the undefined symbols are functions from lib++, your static libraries were compiled with libc++ support.

The only way you can check symbole before linkage stage is checks symbols, which static libraries contain. See this answer about how to do this

Pavel.Zh
  • 437
  • 3
  • 15
  • Thanks, I end up checking it like this `execute_process(COMMAND objdump -tC ${MYLIB_LIBRARIES} COMMAND grep -i std::__1 OUTPUT_VARIABLE HAS_LIBC)` as `__1 inline namespace` does not exist in libstdc++ – afvmil Jun 09 '21 at 07:20
  • Not an answer to the question. You can check with std::__1 for libc++ and with for example std::basic for libstdc++. – N1gthm4r3 Jan 12 '22 at 06:47