为 Ubuntu OSXCross CMake 构建 macOS 应用程序

Written by

in

在这篇文章中,我将描述如何使用 CMake 和 osxcross 在 Ubuntu 构建机器上为 macOS 构建跨平台 C++ 应用程序。
首先,安装osxcross工具链:
https://github.com/tpoechtrager/osxcross
安装分 3 个阶段进行,下载依赖项:

cd tools
./get_dependencies.sh

从Apple官方网站下载XCode.xip,然后从XCode下载SDK:

./gen_sdk_package_pbzx.sh /media/demensdeum/2CE62A79E62A4404/LinuxSupportStorage/xcode111.xip

希望您阅读最后一步中的 XCode 许可协议?接下来,使用所需的前缀构建工具链:

INSTALLPREFIX=/home/demensdeum/Apps/osxcross ./build.sh 

现在您可以使用上一步的前缀目录中的 osxcross。让我们为 CMake 添加一个新的构建宏并编写所有必要的内容:

if (OSXCROSS)
SET(CMAKE_SYSTEM_NAME Darwin)
SET(CMAKE_C_COMPILER o64-clang)
SET(CMAKE_CXX_COMPILER o64-clang++)
SET(CMAKE_C_COMPILER_AR x86_64-apple-darwin19-ar)
SET(CMAKE_CXX_COMPILER_AR x86_64-apple-darwin19-ar)
SET(CMAKE_LINKER x86_64-apple-darwin19-ld)
SET(ENV{OSXCROSS_MP_INC} 1)
endif()

动态链接对我来说并不成功,因此我们静态导出库:

if (OSXCROSS)
add_library(FlameSteelCore STATIC ${SOURCE_FILES})
else()

接下来,您可能会面临这样的情况:您没有 osxcross 所需的库,我在使用 SDL2 时遇到过这种情况。 osxcross 支持现成的库包 – macports。例如,安装 SDL2-mixer:

osxcross-macports -v install libsdl2_mixer

之后,您可以像平常一样在 cmake-make 链接中开始构建库/应用程序,如果需要,不要忘记指定库的静态链接。

手动组装库

目前,我遇到了静态链接时库归档不正确的问题;构建最终应用程序时,我收到错误:

file was built for archive which is not the architecture being linked (x86_64)

与此票证非常相似,我们设法实现了一种解决方法,使构建正确完成。让我们解压缩静态库并使用 osxcross 存档器重新构建它:

ar x ../libFlameSteelCore.a
rm ../libFlameSteelCore.a
x86_64-apple-darwin19-ar rcs ../libFlameSteelCore.a *.o

另外,我个人考虑的问题之一是缺乏在 Ubuntu 上直接运行 macOS 应用程序的能力(至少部分功能)。当然,有一个项目 darling,但支持仍然有很多不足之处。

来源

https://github.com/tpoechtrager/osxcross

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

DemensDeum
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.