{"id":2300,"date":"2019-10-27T09:15:13","date_gmt":"2019-10-27T06:15:13","guid":{"rendered":"http:\/\/demensdeum.com\/blog\/?p=2300"},"modified":"2024-12-16T22:32:33","modified_gmt":"2024-12-16T19:32:33","slug":"crosscompile-for-macos-on-ubuntu-cmake","status":"publish","type":"post","link":"https:\/\/demensdeum.com\/blog\/2019\/10\/27\/crosscompile-for-macos-on-ubuntu-cmake\/","title":{"rendered":"Building macOS applications under Ubuntu OSXCross CMake"},"content":{"rendered":"<p>In this post I will describe building cross-platform C++ applications for macOS on an Ubuntu build machine using CMake and osxcross.<br \/>First, install the osxcross toolchain:<br \/><a href=\"https:\/\/github.com\/tpoechtrager\/osxcross\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/tpoechtrager\/osxcross<\/a><br \/>\nInstallation occurs in 3 stages, downloading dependencies:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>cd tools\n.\/get_dependencies.sh\n<\/code><\/pre>\n<\/div>\n<p>Download XCode.xip from the official Apple website, then unload the SDK from XCode:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>.\/gen_sdk_package_pbzx.sh \/media\/demensdeum\/2CE62A79E62A4404\/LinuxSupportStorage\/xcode111.xip\n<\/code><\/pre>\n<\/div>\n<p>I hope you read the XCode license agreement in the previous step? Next, build the toolchain with the required prefix:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>INSTALLPREFIX=\/home\/demensdeum\/Apps\/osxcross .\/build.sh \n<\/code><\/pre>\n<\/div>\n<p>Now you can use osxcross from the prefix directory of the previous step. Let&#8217;s add a new build macro for CMake, write everything you need:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>if (OSXCROSS)\nSET(CMAKE_SYSTEM_NAME Darwin)\nSET(CMAKE_C_COMPILER o64-clang)\nSET(CMAKE_CXX_COMPILER o64-clang++)\nSET(CMAKE_C_COMPILER_AR x86_64-apple-darwin19-ar)\nSET(CMAKE_CXX_COMPILER_AR x86_64-apple-darwin19-ar)\nSET(CMAKE_LINKER x86_64-apple-darwin19-ld)\nSET(ENV{OSXCROSS_MP_INC} 1)\nendif()\n<\/code><\/pre>\n<\/div>\n<p>Dynamic linking didn&#8217;t work for me, so we export the libraries statically:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>if (OSXCROSS)\nadd_library(FlameSteelCore STATIC ${SOURCE_FILES})\nelse()\n<\/code><\/pre>\n<\/div>\n<p>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 &#8211; macports. For example, installing SDL2-mixer:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>osxcross-macports -v install libsdl2_mixer\n<\/code><\/pre>\n<\/div>\n<p>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.<\/p>\n<h3>Manual assembly of libraries<\/h3>\n<p>At the moment I encountered the problem of incorrect archiving of libraries during static linking, when assembling the final application I get an error:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>file was built for archive which is not the architecture being linked (x86_64)\n<\/code><\/pre>\n<\/div>\n<p>Very similar to <a href=\"https:\/\/github.com\/tpoechtrager\/osxcross\/issues\/11\" rel=\"noopener\" target=\"_blank\">this ticket<\/a>, managed to implement a workaround as a result of which the build completes correctly. Let&#8217;s unzip the static library and rebuild it using the osxcross archiver:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>ar x ..\/libFlameSteelCore.a\nrm ..\/libFlameSteelCore.a\nx86_64-apple-darwin19-ar rcs ..\/libFlameSteelCore.a *.o\n<\/code><\/pre>\n<\/div>\n<p>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 <a href=\"https:\/\/github.com\/darlinghq\/darling\" rel=\"noopener\" target=\"_blank\">darling<\/a> project, but the support leaves much to be desired.<\/p>\n<h3>Sources<\/h3>\n<p><a href=\"https:\/\/github.com\/tpoechtrager\/osxcross\" rel=\"noopener\" target=\"_blank\">https:\/\/github.com\/tpoechtrager\/osxcross<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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<a class=\"more-link\" href=\"https:\/\/demensdeum.com\/blog\/2019\/10\/27\/crosscompile-for-macos-on-ubuntu-cmake\/\">Continue reading <span class=\"screen-reader-text\">&#8220;Building macOS applications under Ubuntu OSXCross CMake&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[61,52],"tags":[126],"class_list":["post-2300","post","type-post","status-publish","format-standard","hentry","category-techie","category-tutorials","tag-osxcross","entry"],"translation":{"provider":"WPGlobus","version":"3.0.2","language":"en","enabled_languages":["en","ru","zh","de","fr","ja","pt"],"languages":{"en":{"title":true,"content":true,"excerpt":false},"ru":{"title":true,"content":true,"excerpt":false},"zh":{"title":true,"content":true,"excerpt":false},"de":{"title":true,"content":true,"excerpt":false},"fr":{"title":true,"content":true,"excerpt":false},"ja":{"title":true,"content":true,"excerpt":false},"pt":{"title":true,"content":true,"excerpt":false}}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/posts\/2300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/comments?post=2300"}],"version-history":[{"count":20,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/posts\/2300\/revisions"}],"predecessor-version":[{"id":3936,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/posts\/2300\/revisions\/3936"}],"wp:attachment":[{"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/media?parent=2300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/categories?post=2300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/tags?post=2300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}