【问题标题】:CMake: How can I check if a STATIC library has been compiled with -stdlib=libc++?CMake:如何检查是否已使用 -stdlib=libc++ 编译了 STATIC 库?
【发布时间】:2021-08-24 10:57:02
【问题描述】:

我想检查我的所有依赖项是否都是使用 libc++ 编译的。如果不是这种情况,则返回警告或错误。我的一些依赖项是共享库,一些是静态的。对于我目前这样做的共享库

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()

我怎样才能为静态库做同样的事情? (主要是 CMake 3.8 及更高版本)

【问题讨论】:

    标签: c++ cmake clang


    【解决方案1】:

    对于静态库,无需检查此项。静态库只是带有目标文件的存档,它们没有运行时依赖项。它们直接链接到您的最终二进制文件。

    您应该阅读有关链接器如何工作的更多信息:

    如果您在没有 libc++ 的情况下编译最终二进制文件并且没有任何“未定义引用”错误 - 一切都很好。否则,如果未定义的符号是来自 lib++ 的函数,则您的静态库是在 libc++ 支持下编译的。

    在链接阶段之前检查符号的唯一方法是检查静态库包含的符号。见this answer about how to do this

    【讨论】:

    • 谢谢,我最终像 execute_process(COMMAND objdump -tC ${MYLIB_LIBRARIES} COMMAND grep -i std::__1 OUTPUT_VARIABLE HAS_LIBC) 这样检查它,因为 __1 inline namespace 在 libstdc++ 中不存在
    猜你喜欢
    • 2012-09-14
    • 1970-01-01
    • 1970-01-01
    • 2013-11-13
    • 1970-01-01
    • 1970-01-01
    • 2021-09-21
    • 2021-08-27
    相关资源
    最近更新 更多