Difference between revisions of "Compiling for Windows on Linux"

From The Powder Toy
Jump to: navigation, search
(Modified commands opening nautilus to use gksu/do. Always run graphical apps with gksu or kdesu!)
(Add outdated notice)
 
(21 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 +
'''ATTENTION: This guide is outdated as of 2021. Please use this guide for building any recent version of the game: [[Building TPT with Meson]]'''
 +
 
This is for Linux users to cross-compile to Windows 32-bit.  
 
This is for Linux users to cross-compile to Windows 32-bit.  
  
Line 10: Line 12:
  
 
'''Ubuntu Users'''
 
'''Ubuntu Users'''
Copy and paste this into your address bar (Firefox):<br>
+
Run the following in a terminal:<br>
''apt:mingw32,mingw32-binutils,mingw32-runtime"
+
<code>sudo apt-get install mingw-w64</code>
  
If it doesn't work, use the following in a terminal:<br>
+
In older recent versions of Ubuntu, try:<br>
<code>sudo apt-get install mingw32 mingw32-binutils mingw32-runtime</code>
+
<code>sudo apt-get install mingw32 mingw32-binutils mingw32-runtime</code>
  
 
'''Debian Users'''
 
'''Debian Users'''
Line 22: Line 24:
 
'''Arch Users'''
 
'''Arch Users'''
 
In your Terminal:<br>
 
In your Terminal:<br>
<code>su -c 'pacman -S mingw32-{gcc,binutils,runtime}</code>
+
<code>su -c 'pacman -S mingw32-{gcc,binutils,runtime}'</code>
 +
 
 +
After you have installed these, there should be /usr/XXXX-mingw32msvc , XXXX may be i486, i586, i686 or something else. Anywhere further in this tutorial i will refer to the folder name as $MINGW , for instance /usr/$MINGW/lib
 +
You could actually set such an environment variable for simplicity.
 +
 
 +
You will also need to enable the posix threading model, instead of Windows. Run each of these manually and select the -posix variants:<br/>
 +
<code>sudo update-alternatives --config i686-w64-mingw32-gcc<br/></code>
 +
<code>sudo update-alternatives --config i686-w64-mingw32-g++<br/></code>
 +
<code>sudo update-alternatives --config x86_64-w64-mingw32-gcc<br/></code>
 +
<code>sudo update-alternatives --config x86_64-w64-mingw32-g++</code>
 +
 
 +
You will have to include libgcc_s_sjlj-1.dll with your .exe.
  
 
=  Libraries  =
 
=  Libraries  =
 
Next, Powder Toy needs a couple of libraries. There are three ways to get them:
 
Next, Powder Toy needs a couple of libraries. There are three ways to get them:
* Package repositories. This is usually the easiest way, but not all distributions have MinGW versions of all the required libraries in their package repositories
+
* Package repositories. This is sometimes the easiest way, but not all distributions have MinGW versions of all the required libraries in their package repositories
* Download and extract precompiled libraries. However, this wiki page does not currently have links for all the libraries.
+
* Download and extract precompiled libraries. However, you usually cannot statically compile when using this way
* Compile from source. This can be difficult, but there is a script available to help you. You'll need to use this method if you don't want to distribute DLL files with your executables.  
+
* Compile from source. This would normally be difficult, but there is a script available to help you. You'll need to use this method if you don't want to distribute DLL files with your executables.  
  
 
The libraries needed are:
 
The libraries needed are:
* SDL
+
* SDL2
 
* bzip2
 
* bzip2
* pthread
+
* Lua (optional - if you don't want to use it, use --nolua option)
* libregex (sometimes found under the name libgnurx)
+
* FFTW (optional - if you don't want to use it, use --nofft option)
* Lua (optional - if you don't want to use it, remove -DLUACONSOLE from the Makefile)
+
* curl (optional - if you don't want to use it, use --nohttp option)
* FFTW (optional - if you don't want to use it, remove -DGRAVFFT from the Makefile)
 
  
== Option 1: Package repositories ==
+
== Compiling Libraries==
When using this method, you will usually need to distribute some DLLs to make your builds work. These are normally located in the ''bin'' folder where MinGW packages were installed (e.g. /usr/i586-mingw32msvc/bin or /usr/i686-w64-mingw32/bin).
+
By compiling all the libraries yourself, the final executable will not need any DLLs to run.
  
'''openSUSE users'''
+
Since figuring out all the right commands to cross compile the libraries can be difficult, here is a script that has all the right commands already: [https://raw.githubusercontent.com/The-Powder-Toy/buildserver-snapshot/master/provisioning-files/cross-libs/cross-libs.sh cross-libs.sh]
  
Some of the required libraries can be found here:
+
Running the script without arguments provides usage instructions. Start by changing the variables at the start of the script to match your MinGW installation. The default values are:<br />
https://build.opensuse.org/project/show?project=windows%3Amingw%3Awin32
+
HOST="i686-w64-mingw32" 
 +
MINGW_BIN_PREFIX="i686-w64-mingw32-" 
 +
MINGW_INSTALL_DIR="/usr/i686-w64-mingw32"
 +
<b>On older versions of Debian/Ubuntu,</b> you might change this to: 
 +
HOST="i586-mingw32msvc"
 +
MINGW_BIN_PREFIX="i586-mingw32msvc-"
 +
MINGW_INSTALL_DIR="/usr/i586-mingw32msvc"
  
Repository URL for package manager:
+
Please note, if you installed MingW for <b>older recent versions of Ubuntu</b> as shown in step 1, you <u>MUST</u> change these variables.
http://download.opensuse.org/repositories/windows:/mingw:/win32/openSUSE_11.4/
 
  
== Option 2: Precompiled ==
+
Now compile and install the libraries as follows (they will automatically be downloaded. Simply open a terminal, and enter the following commands):
=== Download ===
 
Download the libraries (this is an incomplete list, update this wiki page if you find links for the other libraries):
 
  
* [http://www.libsdl.org/release/SDL-devel-1.2.14-mingw32.tar.gz SDL-devel-1.2.14-mingw32.tar.gz] from [http://www.libsdl.org/ http://www.libsdl.org/]
+
<code>./cross-libs.sh make bzip2 fftw lua regex sdl2 zlib curl</code>
* [http://sourceforge.net/projects/mingw/files/MinGW/Extension/bzip2/bzip2-1.0.6-4/bzip2-1.0.6-4-mingw32-dev.tar.lzma/download bzip2-1.0.6-4-mingw32-dev.tar.lzma] from [http://sourceforge.net/projects/mingw/files/MinGW/Extension/bzip2/ MinGW on SourceForge].
 
* [http://sourceforge.net/projects/mingw/files/Other/UserContributed/regex/mingw-regex-2.5.1/mingw-libgnurx-2.5.1-dev.tar.gz/download mingw-libgnurx-2.5.1-dev.tar.gz] from [http://sourceforge.net/projects/mingw/files/Other/UserContributed/regex/ MinGW on SourceForge]
 
  
===  Extract  ===
+
<code>sudo ./cross-libs.sh install bzip2 fftw lua regex sdl2 zlib curl</code>
'''THE EXTRACTION PROCESS CAN ONLY BE DONE AS ROOT USER. USE AN ALTERNATE WORKSPACE AND CLOSE ALL WINDOWS WHEN FINISHED.'''
 
  
GNOME:
+
If the script doesn't run, you need to allow the file to be run as an executable. To do this, use <code>chmod +x cross-libs.sh</code>
<code>gksudo nautilus
 
-OR-
 
gksu -w nautilus</code>\\
 
XFCE use Thunar instead of Nautilus\\
 
KDE use Konqueror and kdesudo instead of gksudo.
 
  
The target folder for all extractions is /usr/i586-mingw32msvc (Arch use /usr/i486-mingw32). Everything I'm telling you to extract is a folder and any overwrites or merges should be permitted.
+
Compile =
 
+
It should be as simple as
===SDL-devel-1.2.14-mingw32.tar.gz ====
+
<code>scons --win</code>
Extract the bin, include and libs folders
 
 
 
====  bzip2-1.0.6-4-mingw32-dev.tar.lzma  ====
 
Extract the entire archive.
 
  
====  mingw-libgnurx-2.5.1-dev.tar.gz  ====
+
Some SConscript changes may be needed if the name of your cross compiler isn't standard (or just not listed in the SConscript). If it does not detect your cross compiler, try using:
Extract the entire archive.
+
<code>scons --win --tool=$MINGW-</code><br />
 +
Notice the dash, if for you $MINGW is i586-mingw32msvc you should pass <code>--tool=i586-mingw32msvc-</code>.
  
===  DLLs  ===
+
If you have problems with initial libraries lookup, it is useful to check config.log for obvious failures.
These DLLs will need to be distributed with your builds so they work. Or if you're lazy you can just  post the URLs and tell everyone else to download them.
 
  
* http://tinyurl.com/sdldll - SDL.dll
+
If when linking it gives you an error like
* http://tinyurl.com/bz2dll - libbz2-2.dll
 
* http://tinyurl.com/grxdll - libgnurx-0.dll
 
 
 
 
 
== Option 3: Building from source ==
 
This is the method to use if you want to use static linking (so that the executable does not need extra DLL files to run).
 
 
 
Since figuring out all the right commands to cross compile the libraries can be difficult, here is a script that (hopefully) has all the right commands already: https://raw.github.com/jacksonmj/The-Powder-Toy/master/powder-cross-libs.sh
 
 
 
Running the script without arguments provides usage instructions. Start by changing the variables at the start of the script to match your MinGW installation. Then compile and install the libraries as follows (they will automatically be downloaded):
 
 
 
<code>./powder-cross-libs.sh make bzip2 fftw lua pthread regex sdl</code>
 
 
 
<code>sudo ./powder-cross-libs.sh install bzip2 fftw lua pthread regex sdl</code>
 
 
 
=  Compile  =
 
Some Makefile changes may be needed.
 
  
CC_WIN and WIN_RES should be set to the names of the relevant MinGW programs. These names vary between distributions.
+
<code>/usr/lib/gcc/i586-mingw32msvc/4.2.1-sjlj/libstdc++.a(stubs.o):(.text+0x540): multiple definition of `_coshf'
  
Arch users:
+
build/src/Misc.o:Misc.cpp:(.text$coshf[_coshf]+0x0): first defined here</code>
  
<code>CC_WIN := i486-mingw32-gcc<br>
+
do this (under root):
WIN_RES := i486-mingw32-windres</code>
 
  
Change all instances of ''-llibregex'' to ''-llibgnurx''
+
<code>cd /usr/lib/gcc/$MINGW/4.2.1-sjlj/
  
Save, Exit, cd to your powder toy directory and run:
+
$MINGW-ar -d libstdc++.a stubs.o</code>
<code>make powder-sse.exe powder-sse2.exe powder-sse3.exe</code>
 
  
 +
If this tutorial has any obvious (and not very obvious) failures, feel free to edit.
 
[[Category:Development]]
 
[[Category:Development]]
 
[[Category:Compiling]]
 
[[Category:Compiling]]

Latest revision as of 08:58, 3 April 2021

ATTENTION: This guide is outdated as of 2021. Please use this guide for building any recent version of the game: Building TPT with Meson

This is for Linux users to cross-compile to Windows 32-bit.

BEFORE WE START, PLEASE MAKE SURE YOU CAN COMPILE FOR LINUX FIRST!

Packages

First, install the basic packages needed for cross compiling.

Search for the following in your package manager: mingw32 mingw32-binutils mingw32-runtime

Ubuntu Users Run the following in a terminal:
sudo apt-get install mingw-w64

In older recent versions of Ubuntu, try:
sudo apt-get install mingw32 mingw32-binutils mingw32-runtime

Debian Users In a terminal:
su -c "apt-get install mingw32 mingw32-binutils mingw32-runtime"

Arch Users In your Terminal:
su -c 'pacman -S mingw32-{gcc,binutils,runtime}'

After you have installed these, there should be /usr/XXXX-mingw32msvc , XXXX may be i486, i586, i686 or something else. Anywhere further in this tutorial i will refer to the folder name as $MINGW , for instance /usr/$MINGW/lib You could actually set such an environment variable for simplicity.

You will also need to enable the posix threading model, instead of Windows. Run each of these manually and select the -posix variants:
sudo update-alternatives --config i686-w64-mingw32-gcc
sudo update-alternatives --config i686-w64-mingw32-g++
sudo update-alternatives --config x86_64-w64-mingw32-gcc
sudo update-alternatives --config x86_64-w64-mingw32-g++

You will have to include libgcc_s_sjlj-1.dll with your .exe.

Libraries

Next, Powder Toy needs a couple of libraries. There are three ways to get them:

  • Package repositories. This is sometimes the easiest way, but not all distributions have MinGW versions of all the required libraries in their package repositories
  • Download and extract precompiled libraries. However, you usually cannot statically compile when using this way
  • Compile from source. This would normally be difficult, but there is a script available to help you. You'll need to use this method if you don't want to distribute DLL files with your executables.

The libraries needed are:

  • SDL2
  • bzip2
  • Lua (optional - if you don't want to use it, use --nolua option)
  • FFTW (optional - if you don't want to use it, use --nofft option)
  • curl (optional - if you don't want to use it, use --nohttp option)

Compiling Libraries

By compiling all the libraries yourself, the final executable will not need any DLLs to run.

Since figuring out all the right commands to cross compile the libraries can be difficult, here is a script that has all the right commands already: cross-libs.sh

Running the script without arguments provides usage instructions. Start by changing the variables at the start of the script to match your MinGW installation. The default values are:

HOST="i686-w64-mingw32"  
MINGW_BIN_PREFIX="i686-w64-mingw32-"  
MINGW_INSTALL_DIR="/usr/i686-w64-mingw32" 

On older versions of Debian/Ubuntu, you might change this to:

HOST="i586-mingw32msvc"
MINGW_BIN_PREFIX="i586-mingw32msvc-"
MINGW_INSTALL_DIR="/usr/i586-mingw32msvc"

Please note, if you installed MingW for older recent versions of Ubuntu as shown in step 1, you MUST change these variables.

Now compile and install the libraries as follows (they will automatically be downloaded. Simply open a terminal, and enter the following commands):

./cross-libs.sh make bzip2 fftw lua regex sdl2 zlib curl

sudo ./cross-libs.sh install bzip2 fftw lua regex sdl2 zlib curl

If the script doesn't run, you need to allow the file to be run as an executable. To do this, use chmod +x cross-libs.sh

Compile

It should be as simple as scons --win

Some SConscript changes may be needed if the name of your cross compiler isn't standard (or just not listed in the SConscript). If it does not detect your cross compiler, try using: scons --win --tool=$MINGW-
Notice the dash, if for you $MINGW is i586-mingw32msvc you should pass --tool=i586-mingw32msvc-.

If you have problems with initial libraries lookup, it is useful to check config.log for obvious failures.

If when linking it gives you an error like

/usr/lib/gcc/i586-mingw32msvc/4.2.1-sjlj/libstdc++.a(stubs.o):(.text+0x540): multiple definition of `_coshf'

build/src/Misc.o:Misc.cpp:(.text$coshf[_coshf]+0x0): first defined here

do this (under root):

cd /usr/lib/gcc/$MINGW/4.2.1-sjlj/

$MINGW-ar -d libstdc++.a stubs.o

If this tutorial has any obvious (and not very obvious) failures, feel free to edit.