{"id":2285,"date":"2019-10-18T09:17:02","date_gmt":"2019-10-18T06:17:02","guid":{"rendered":"http:\/\/demensdeum.com\/blog\/?p=2285"},"modified":"2024-12-16T22:32:33","modified_gmt":"2024-12-16T19:32:33","slug":"crosscompile-for-window-on-ubuntu-mingw-cmake","status":"publish","type":"post","link":"https:\/\/demensdeum.com\/blog\/2019\/10\/18\/crosscompile-for-window-on-ubuntu-mingw-cmake\/","title":{"rendered":"Build for Windows under Ubuntu MinGW CMake"},"content":{"rendered":"<p>In this note I will describe the process of building libraries and applications for Windows using the MinGW32 toolchain on Ubuntu.<br \/>Install wine, mingw:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>sudo apt-get install wine mingw-w64\n<\/code><\/pre>\n<\/div>\n<p>After this, you can already compile C\/C++ applications for Windows:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code># C\ni686-w64-mingw32-gcc helloWorld.c -o helloWorld32.exe      # 32-bit\nx86_64-w64-mingw32-gcc helloWorld.c -o helloWorld64.exe    # 64-bit\n \n# C++\ni686-w64-mingw32-g++ helloWorld.cc -o helloWorld32.exe     # 32-bit\nx86_64-w64-mingw32-g++ helloWorld.cc -o helloWorld64.exe   # 64-bit\n<\/code><\/pre>\n<\/div>\n<p>The compiled exe can be checked using wine.<\/p>\n<p>Next, let&#8217;s look at the changes to the CMake build, the CMakeLists.txt file, and add MinGW-specific things to the build file:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>if (MINGW32)\nset(CMAKE_SYSTEM_NAME Windows)\nSET(CMAKE_C_COMPILER i686-w64-mingw32-gcc)\nSET(CMAKE_CXX_COMPILER i686-w64-mingw32-g++)\nSET(CMAKE_RC_COMPILER i686-w64-mingw32-windres)\nset(CMAKE_RANLIB i686-w64-mingw32-ranlib)\nendif()\n\n\/\/ \u0434\u043b\u044f \u0441\u0431\u043e\u0440\u043a\u0438 shared dll\nelseif (MINGW32)\nadd_library(FlameSteelEngineGameToolkit.dll SHARED ${SOURCE_FILES})\nelse()\n\n\/\/ \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043b\u0438\u043d\u043a\u0443\u0435\u043c \u0441\u043e \u0432\u0441\u0435\u043c\u0438 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044f\u043c\u0438\nif (MINGW32)\ntarget_link_libraries(\n                        FlameSteelEngineGameToolkit.dll \n                        -static-libgcc\n                        -static-libstdc++\n                        SDL2 \n                        SDL2_mixer \n                        \/home\/demensdeum\/Sources\/cube-art-project-bootstrap\/FlameSteelFramework\/FlameSteelCore\/FlameSteelCore.dll\n                        \/home\/demensdeum\/Sources\/cube-art-project-bootstrap\/FlameSteelFramework\/FlameSteelBattleHorn\/FlameSteelBattleHorn.dll\n                        \/home\/demensdeum\/Sources\/cube-art-project-bootstrap\/FlameSteelFramework\/FlameSteelCommonTraits\/FlameSteelCommonTraits.dll)\n\nset_target_properties(FlameSteelEngineGameToolkit.dll PROPERTIES\n        PREFIX \"\"\n        SUFFIX \"\"\n        LINK_FLAGS \"-Wl,--add-stdcall-alias\"\n        POSITION_INDEPENDENT_CODE 0 # this is to avoid MinGW warning; \n        # MinGW generates position-independent-code for DLL by default\n)\nelse()\n<\/code><\/pre>\n<\/div>\n<p>We collect:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>cmake -DMINGW32=1 .\nmake\n<\/code><\/pre>\n<\/div>\n<p>The output will be dll or exe, depending on what you are building. For a working example, you can look at the repository of the new Cube-Art-Project and its libraries:<br \/><a href=\"https:\/\/gitlab.com\/demensdeum\/cube-art-project\" rel=\"noopener noreferrer\" target=\"_blank\">https:\/\/gitlab.com\/demensdeum\/cube-art-project<\/a><br \/>\n<a href=\"https:\/\/gitlab.com\/demensdeum\/FlameSteelEngineGameToolkitFSGL\" rel=\"noopener noreferrer\" target=\"_blank\">https:\/\/gitlab.com\/demensdeum\/FlameSteelEngineGameToolkitFSGL<\/a><br \/>\n<a href=\"https:\/\/gitlab.com\/demensdeum\/cube-art-project-bootstrap\" rel=\"noopener noreferrer\" target=\"_blank\">https:\/\/gitlab.com\/demensdeum\/cube-art-project-bootstrap<\/a><\/p>\n<p>Sources<br \/><a href=\"https:\/\/arrayfire.com\/cross-compile-to-windows-from-linux\/\" rel=\"noopener noreferrer\" target=\"_blank\">https:\/\/arrayfire.com\/cross-compile-to-windows-from-linux\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this note I will describe the process of building libraries and applications for Windows using the MinGW32 toolchain on Ubuntu.Install wine, mingw: sudo apt-get install wine mingw-w64 After this, you can already compile C\/C++ applications for Windows: # C i686-w64-mingw32-gcc helloWorld.c -o helloWorld32.exe # 32-bit x86_64-w64-mingw32-gcc helloWorld.c -o helloWorld64.exe # 64-bit # C++ i686-w64-mingw32-g++<a class=\"more-link\" href=\"https:\/\/demensdeum.com\/blog\/2019\/10\/18\/crosscompile-for-window-on-ubuntu-mingw-cmake\/\">Continue reading <span class=\"screen-reader-text\">&#8220;Build for Windows under Ubuntu MinGW 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":[],"class_list":["post-2285","post","type-post","status-publish","format-standard","hentry","category-techie","category-tutorials","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\/2285","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=2285"}],"version-history":[{"count":13,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/posts\/2285\/revisions"}],"predecessor-version":[{"id":3937,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/posts\/2285\/revisions\/3937"}],"wp:attachment":[{"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/media?parent=2285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/categories?post=2285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/tags?post=2285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}