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 download 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 last 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 and write everything necessary:
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 was not successful for me, so we export the libraries statically:
if (OSXCROSS)
add_library(FlameSteelCore STATIC ${SOURCE_FILES})
else()
Next, you may be faced with 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 building libraries/applications as usual in the cmake-make link, do not forget to specify static linking of libraries if necessary.
Manual assembly of libraries
Currently, I have encountered the problem of incorrect archiving of libraries during static linking; when building the final application I receive the error:
file was built for archive which is not the architecture being linked (x86_64)
Very similar to this ticket, we managed to implement a workaround resulting in the build completing correctly. Let’s unzip the static library and build it anew using the osxcross archiver:
ar x ../libFlameSteelCore.a
rm ../libFlameSteelCore.a
x86_64-apple-darwin19-ar rcs ../libFlameSteelCore.a *.o
Also, one of the problems I personally consider is the lack of ability to run macOS applications directly on Ubuntu (at least with part of the functionality). Of course, there is a project darling, but the support still leaves much to be desired.
Leave a Reply