如何在使用CMake / CPack制作的Mac OS捆绑软件中包含特定版本的C ++库?

时间:2019-10-20 03:36:48

标签: c++ cmake cpack

我创建了一个用C ++ 17编写的应用程序,并在Mac上使用最新的编译器和Catalina版本的MacOS进行了编译。我正在使用CMake和CPack创建捆绑并将其打包到.dmg中。 当我使用Catalina在另一台计算机上获取生成的文件时,一切正常。但是,当尝试在具有MacOS 10.12的计算机上运行时,会出现错误:

dyld: Symbol not found: __ZNSt19bad_optional_accessD1Ev
Expected in: /usr/lib/libc++.1.dylib

在分解符号时,它实际上是bad_optional_access异常的析构函数:std::bad_optional_access::~bad_optional_access()

std::optional是C ++ 17中引入的。因此,装有MacOS 10.12的计算机的libc ++中没有该符号。我正在尝试找出如何将C ++ 17的libc++.1.dylib打包到捆绑软件中,这样它才不会使用系统一。

要创建我的捆绑包,

# Installation
set(prefix "${PROJECT_NAME}.app/Contents")
set(INSTALL_RUNTIME_DIR "${prefix}/MacOS")
set(INSTALL_CMAKE_DIR "${prefix}/Resources")
set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR})

# based on code from CMake's QtDialog/CMakeLists.txt
macro(install_qt5_plugin _qt_plugin_name _qt_plugins_var _prefix)
    get_target_property(_qt_plugin_path "${_qt_plugin_name}" LOCATION)
    if(EXISTS "${_qt_plugin_path}")
        get_filename_component(_qt_plugin_file "${_qt_plugin_path}" NAME)
        get_filename_component(_qt_plugin_type "${_qt_plugin_path}" PATH)
        get_filename_component(_qt_plugin_type "${_qt_plugin_type}" NAME)
        set(_qt_plugin_dest "${_prefix}/PlugIns/${_qt_plugin_type}")
        install(FILES "${_qt_plugin_path}"
                DESTINATION "${_qt_plugin_dest}")
        set(${_qt_plugins_var}
                "${${_qt_plugins_var}};\$ENV{DEST_DIR}\${CMAKE_INSTALL_PREFIX}/${_qt_plugin_dest}/${_qt_plugin_file}")
    else()
        message(FATAL_ERROR "QT plugin ${_qt_plugin_name} not found")
    endif()
endmacro()

install_qt5_plugin("Qt5::QCocoaIntegrationPlugin" QT_PLUGINS ${prefix})
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/qt.conf"
        "[Paths]\nPlugins = ${_qt_plugin_dir}\n")
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/qt.conf"
        DESTINATION "${INSTALL_CMAKE_DIR}")

# Destination paths below are relative to ${CMAKE_INSTALL_PREFIX}
install(TARGETS ${PROJECT_NAME}
        BUNDLE DESTINATION . COMPONENT Runtime
        RUNTIME DESTINATION ${INSTALL_RUNTIME_DIR} COMPONENT Runtime
        )

# Note Mac specific extension .app
set(APPS "\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${PROJECT_NAME}.app")

# Directories to look for dependencies
set(DIRS "${CMAKE_BINARY_DIR}")

# Path used for searching by FIND_XXX(), with appropriate suffixes added
if(CMAKE_PREFIX_PATH)
    foreach(dir ${CMAKE_PREFIX_PATH})
        list(APPEND DIRS "${dir}/bin" "${dir}/lib")
    endforeach()
endif()


list(APPEND DIRS "$ENV{VULKAN_SDK}/lib")
list(APPEND QT_PLUGINS "${APPS}/Contents/Resources/MoltenVk/lib/libMoltenVK.dylib")
# Append Qt's lib folder which is two levels above Qt5Widgets_DIR
list(APPEND DIRS "${Qt5Widgets_DIR}/../..")

include(InstallRequiredSystemLibraries)

message(STATUS "APPS: ${APPS}")
message(STATUS "QT_PLUGINS: ${QT_PLUGINS}")
message(STATUS "DIRS: ${DIRS}")

install(CODE "include(BundleUtilities)
    fixup_bundle(\"${APPS}\" \"${QT_PLUGINS}\" \"${DIRS}\")")

install(CODE "include(BundleUtilities)
    fixup_bundle(\"${APPS}\" \"${MOLTEN_LIB}\" \"${DIRS}\")")

set(CPACK_GENERATOR "DRAGNDROP")
include(CPack)

对于Qt Vulkan和MoltenVK库,它的工作正确。我能够在没有安装Vulkan和MoltenVk库的运行Catalina的另一台计算机上运行,​​并且在可执行文件上运行otool -L表示该计算机正在使用嵌入式Qt库。但是,它是指/usr/lib/libc++.1.dylib库。

我不明白如何正确地嵌入仅在系统文件夹/ usr / lib中找到的C ++标准库,而不包括该文件夹中的其他库,并确保fixup_bundle做正确的更新该库的链接。 如果有人知道如何正确执行此操作,我将不胜感激。

0 个答案:

没有答案