In this post I will describe building cross-platform C++ applications for macOS on an Ubuntu build machine using CMake and osxcross.
First, install the osxcross toolchain:
https://github.com/tpoechtrager/osxcross
Installation occurs in 3 stages, downloading dependencies:
cd tools
./get_dependencies.sh
Download XCode.xip from the official Apple website, then unload the SDK from XCode:
./gen_sdk_package_pbzx.sh /media/demensdeum/2CE62A79E62A4404/LinuxSupportStorage/xcode111.xip
I hope you read the XCode license agreement in the previous step? Next, build the toolchain with the required prefix:
INSTALLPREFIX=/home/demensdeum/Apps/osxcross ./build.sh
Now you can use osxcross from the prefix directory of the previous step. Let’s add a new build macro for CMake, write everything you need:
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()
Dynamic linking didn’t work for me, so we export the libraries statically:
if (OSXCROSS)
add_library(FlameSteelCore STATIC ${SOURCE_FILES})
else()
Next you may encounter the fact that you do not have the necessary libraries for osxcross, I encountered this when using SDL2. osxcross supports ready-made library packages – macports. For example, installing SDL2-mixer:
osxcross-macports -v install libsdl2_mixer
After this, you can start assembling libraries/applications as usual in the cmake-make bundle, do not forget to register static linking of libraries if necessary.
Manual assembly of libraries
At the moment I encountered the problem of incorrect archiving of libraries during static linking, when assembling the final application I get an error:
file was built for archive which is not the architecture being linked (x86_64)
Very similar to this ticket, managed to implement a workaround as a result of which the build completes correctly. Let’s unzip the static library and rebuild it using the osxcross archiver:
ar x ../libFlameSteelCore.a
rm ../libFlameSteelCore.a
x86_64-apple-darwin19-ar rcs ../libFlameSteelCore.a *.o
Also, I personally think that one of the problems is the lack of the ability to run macOS applications directly on Ubuntu (at least with some of the functionality), of course there is the darling project, but the support leaves much to be desired.
Sources
https://github.com/tpoechtrager/osxcross