Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1.  
  2. Yaseen Eltii
  3. 21 May 2015
  4. Setting Up a Linux -> Windows Cross Compilation Environment
  5.  
  6. Sometimes you are developing some cross platform application, and maybe using Linux as your Development platform, you may not have Windows installed or just too lazy to switch. This should be a solution for you.
  7. In this tutorial we are going to set up a cross compilation environment on Linux to compile applications for Windows. At the end if the tutorial you'll have something like this
  8.  
  9.  
  10. Setting Up MXE ( MinGW )
  11.  
  12. First lets set up our cross compiler. We can simply do that by installing mingw-w64, but then we'll have to compile other libraries ourself .. thank god there's a script to do that for us ( Well a Makefile .. ) called MXE.
  13. MXE simply compiles mingw with a lot of other libraries (zlib, boost, sdl, .. etc ), doing that manually would be a tough job.
  14. first go ahead install git if you don't already have it, replace apt-get with whatever package manager your dist uses ex : yum.
  15.  
  16. sudo apt-get install git
  17.  
  18. then clone the MXE repository.
  19.  
  20. git clone https://github.com/mxe/mxe
  21.  
  22. MXE by default only sets up a x32 compiler, if you also want a x64 one go to your cloned directory and open settings.mk with your favorite editor and uncomment this line :
  23.  
  24. x86_64-w64-mingw32.static x86_64-w64-mingw32.shared
  25.  
  26. now open a terminal and change to your cloned directory :
  27.  
  28. cd /path/to/mxe
  29.  
  30. and then run :
  31.  
  32. make
  33.  
  34. this will take a long time to finish, even up to hours because it downloads and compiles a lot of libraries depending on your CPU and Internet connection. If you don't need all of the libraries for now just run :
  35.  
  36. make gcc qtbase
  37.  
  38. and when ever you need something else just make again
  39. ex : make boost gtk, Heres a full list of what libraries can MXE compile.
  40.  
  41. Now you actually have a cross compiler set up, but we also want an IDE .
  42. Before setting up an IDE lets add the new compiler to the environment path. open /etc/environemnt with an editor with root privilege .
  43.  
  44. sudo gedit /etc/environment
  45.  
  46. Add the path of the compiler executables, if you cloned the repo in your home directory it should be :
  47.  
  48. /home/someusername/mxe/usr/bin/
  49.  
  50. So now your environment file should look something like this :
  51.  
  52. PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/username/mxe/usr/bin"
  53.  
  54. This wont take effect until you restart your computer or at least run source /etc/environment for the current terminal session.
  55. now executing i686-w64-mingw32.static-g++ should work.
  56. You can continue without it anyways, unless your going to compile from the terminal, in that case you'll have to specify the full path of the compiler.
  57. Setting Up QtCreator
  58.  
  59. QTCreator is an Awesome C++ IDE. I use it for all of my development (and no it doesn't need to be a qt application).
  60.  
  61. you can installing with :
  62.  
  63. sudo apt-get install qtcreator
  64.  
  65. Go ahead and open it. First thing I do when installing an IDE is change it to the Dark Color, because it looks much better you can skip this if you want.
  66.  
  67. Tools > Options > Text Editor > Color Scheme = Inkpot
  68.  
  69. For our IDE to work we need to set up qmake, so go back to your mxe directory
  70.  
  71. /path/to/mxe/usr/bin
  72.  
  73. and find a file called i686-w64-mingw32.static-qmake rename it to qmake or else qtcreator wont find it
  74.  
  75. Now go to Tools > Options > Build & Run > Qt Versions > Add
  76. Select the file we just renamed and then give the version a name something like QT_WIN_32 .
  77.  
  78. Next swicth to Compilers tab and press Add > MinGW
  79. name your new compiler MinGW_32 or similar then choose the path it should be like this if you have mxe in your home.
  80. /home/username/mxe/usr/bin/i686-w64-mingw32.statix-g++
  81.  
  82. if the ABI is not set like this change it to custom and set it up by your self x86-windows-mysys-pe-32bit or x86-64/64 for x64.
  83.  
  84. Last thing go to the Kits tab and press Add call your kit Win32 change the compiler to your new compiler, Debugger to none and QtVersion to the qt version we just set up and then press OK .. it OK if there a warning triangle .
  85.  
  86. Actually now your all setup to compile your apps for windows you can test this by Creating a New Project
  87. File > New File Or Project > Non-Qt-Project > Plain C or C++ Application. Name it App or such and press Next here you have to select the Win32 Kit We setup earlier , Next Then Finish ..
  88. Add this simple example code :
  89.  
  90. #include <windows.h>
  91.  
  92. int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
  93. {
  94. MessageBoxA(NULL, "Compiled On Linux :D", "Cross Compiled", MB_OK);
  95. return(0);
  96. }
  97.  
  98. Press the hammer tool to build and here you go you have a compiled exe :D .. you cant really test on Linux unless you have wine installed , Wine should work well with normal programs but for advanced project it may be buggy or may not work at all ..
  99. Setting up Wine
  100.  
  101. This is actually pretty simple just run
  102. sudo apt-get install wine
  103.  
  104. not navigate to your exe directory and double click it :D Yaaaay !!, It Works :D .
  105.  
  106. Thats it your all set up .
  107. Notes :
  108.  
  109. xxx-minge-xx.static is for static linking and the .shared if for shared, you can have one compiler for both by using removing .static or .shared from the names in the setting.mk but for some what reason it'll say you using the deprecated version but I haven't got any problems with it though.
  110. If you want a 64 bit Kit and you cant rename both qmake's to qmake since they are in the same directory just move one of them to a new folder
  111.  
  112. Yaseen Mohamed Twati
  113.  
  114. Read More Posts by this author.
  115. Share this post
  116.  
  117. Contact About
  118.  
  119. Yaseen Eltii
  120. Subscribe!
  121. Yaseen Eltii © 2015
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement