5

I have created an application that is written in C++17 and compiled on a Mac with the latest compiler and Catalina version of MacOS. I am using CMake and CPack to create a bundle and package it into a .dmg. When I take the resulting file on another computer with Catalina, then everything works. But when trying to run on a machine that does have MacOS 10.12, then there is an error:

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

When demangling the symbol, it is actually the destructor for bad_optional_access exception: std::bad_optional_access::~bad_optional_access()

The std::optional was introduced in C++17. So the computer with MacOS 10.12 does not have that symbol in its libc++. I'm trying to find out how to package the libc++.1.dylib for C++17 into the bundle so that it would not use the system one.

To create my bundle I have:

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

It does the right job for the Qt Vulkan and MoltenVK libraries. I am able to run on another computer running Catalina on which the Vulkan and MoltenVk libraries are not installed and running otool -L on the executable shows it is using the embedded Qt libraries. It is, however referring to the /usr/lib/libc++.1.dylib library.

I do not understand how to properly embed just the C++ standard library which is found in the system folder /usr/lib and not include other libraries from that folder and make sure fixup_bundle does the right job of updating the links for that library. I would appreciate some help if someone knows how to do this properly.

Kevin
  • 16,549
  • 8
  • 60
  • 74
  • By installing g++ and using it as the compiler, its C++ library gets included. It is not a great solution however because g++ STL does not have all the versions of exclusive_scan which I use. – Michel Oct 31 '19 at 01:53
  • 2
    This is a great question and I don't get why no-one answers this or even searches for it. We all need C++17+ and still want to target macOS<10.14. Have you found any solution yet? – IceFire Aug 27 '20 at 06:07

0 Answers0