• Create Account

# Crosscompiling from Linux to Windows

5 replies to this topic

### #1Sik_the_hedgehog  Crossbones+   -  Reputation: 2245

Like
0Likes
Like

Posted 31 July 2014 - 05:38 AM

OK, so I want to make Windows builds of my game but I'm running Ubuntu. I was expecting to get a Windows computer this month (which I was planning to use to make Windows builds) but that seems like it isn't going to happen so I'm probably stuck trying to crosscompile from Ubuntu instead. Already installed MinGW-w64 (through Software Center, so assume default everything), but I have no idea how to use it.

Basically I need to build these libraries:

• SDL 2
• PhysicsFS
• libpng
• zlib
• libogg
• libvorbis
• vorbisfile

PhysicsFS uses CMake so that's probably easy (just tell CMake to use MinGW-w64 to build). SDL2 includes a CMake script too so I could attempt to use that (not sure how well it works though). The rest of the libraries I have absolutely no idea how, though.

Then after that I need to get the game build for Windows. Again, no idea. Currently using Code::Blocks. I noticed MinGW-w64 isn't detected, but I wonder if I could just clone the GCC settings and change the directories (since otherwise it seems to be identical). If somebody else has a better solution tell me. Otherwise I may need to make a makefile and I have no idea how to make it to work properly with the crosscompiler.

Any help is appreciated! (this is also to learn how to use a crosscompiler!)

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

### #2Tribad  Members   -  Reputation: 980

Like
0Likes
Like

Posted 31 July 2014 - 05:44 AM

In the configuration script that mostly come with the source packages use the --host --target parameters

But better have a look in the lists of parameters with ./configure --help

It may work.

### #3Sik_the_hedgehog  Crossbones+   -  Reputation: 2245

Like
0Likes
Like

Posted 31 July 2014 - 06:22 AM

Yeah, but I have no idea what to fill in those parameters (since I never used configure before except as "./configure"). Also, zlib isn't being very helpful:

sik@host221:~/Descargas/zlib-1.2.8$./configure --help usage: configure [--const] [--zprefix] [--prefix=PREFIX] [--eprefix=EXPREFIX] [--static] [--64] [--libdir=LIBDIR] [--sharedlibdir=LIBDIR] [--includedir=INCLUDEDIR] [--archs="-arch i386 -arch x86_64"] Either it's not listing all possible parameters, or it's not making it easy to crosscompile. Odd, considering what kind of library it is. Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick. ### #4samoth Crossbones+ - Reputation: 6671 Like 2Likes Like Posted 31 July 2014 - 07:12 AM Currently using Code::Blocks. I noticed MinGW-w64 isn't detected, but I wonder if I could just clone the GCC settings and change the directories (since otherwise it seems to be identical). That works, but you must change the executable prefix, not the path (they're normally all in the same location). Your /usr/bin folder looks something like this (that's mine, actually): me@ubuntu:/usr$ ls -l bin | grep gcc
-rwxr-xr-x 1 root root         428 Mai  7  2006 c89-gcc
-rwxr-xr-x 1 root root         454 Apr 11  2011 c99-gcc
lrwxrwxrwx 1 root root           7 Okt 12  2013 gcc -> gcc-4.8
-rwxr-xr-x 1 root root      357344 Jun 19  2013 gcc-4.6
-rwxr-xr-x 1 root root      775888 Nov 15  2013 gcc-4.8
-rwxr-xr-x 1 root root       26968 Nov 15  2013 gcc-ar-4.8
-rwxr-xr-x 1 root root       26968 Nov 15  2013 gcc-nm-4.8
-rwxr-xr-x 1 root root       26968 Nov 15  2013 gcc-ranlib-4.8
-rwxr-xr-x 1 root root      349248 Dez 13  2012 i686-w64-mingw32-gcc
-rwxr-xr-x 1 root root      349256 Dez 13  2012 i686-w64-mingw32-gcc-4.6
lrwxrwxrwx 1 root root           7 Okt 12  2013 x86_64-linux-gnu-gcc -> gcc-4.8
lrwxrwxrwx 1 root root           7 Jun 19  2013 x86_64-linux-gnu-gcc-4.6 -> gcc-4.6
lrwxrwxrwx 1 root root           7 Nov 15  2013 x86_64-linux-gnu-gcc-4.8 -> gcc-4.8
lrwxrwxrwx 1 root root          10 Nov 15  2013 x86_64-linux-gnu-gcc-ar-4.8 -> gcc-ar-4.8
lrwxrwxrwx 1 root root          10 Nov 15  2013 x86_64-linux-gnu-gcc-nm-4.8 -> gcc-nm-4.8
lrwxrwxrwx 1 root root          14 Nov 15  2013 x86_64-linux-gnu-gcc-ranlib-4.8 -> gcc-ranlib-4.8
-rwxr-xr-x 1 root root      349248 Dez 13  2012 x86_64-w64-mingw32-gcc
-rwxr-xr-x 1 root root      349256 Dez 13  2012 x86_64-w64-mingw32-gcc-4.6

Note how all the cross compilers (including the x64 Linux "cross" compiler, which is really just a symlink to the native compiler that doesn't have a prefix) have a different name prefix.

Of course that's only for making Code::Blocks work, for cmake or configure you need to do the obscure build/host/target triplet dance.

Unluckily, the GNU build system is where it gets nasty, even after years, I still need several attempts to get it right every single time I try. From the official docs:

The relationship between build, host, and target have been cleaned up: the chain of default is now simply: target defaults to host, host to build, and build to the result of config.guess. [...]

configure enters cross-compilation mode if and only if --host is passed. That's the short documentation.

Now of course, it also tells you further down that you are not to provide host without build, since that is "fragile" (whatever that means). Great, huh. So, in short, you need to provide these:

--build=... The system on which the package is built. That is, x86_64-linux-gnu-gcc or whatever it is on your box (probably exactly that!).
--host=...  The system where built programs and libraries will run. That's x86_64-w64-mingw32-gcc or whatever you choose it to be.

me@ubuntu:/usr\$ x86_64-w64-mingw32-gcc -v
Using built-in specs.
COLLECT_GCC=x86_64-w64-mingw32-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-w64-mingw32/4.6/lto-wrapper
Target: x86_64-w64-mingw32
Configured with: ../../src/configure --build=x86_64-linux-gnu --prefix=/usr --includedir='/usr/include'
--mandir='/usr/share/man' --infodir='/usr/share/info' --sysconfdir=/etc --localstatedir=/var
--libexecdir='/usr/lib/gcc-mingw-w64' --disable-maintainer-mode --disable-dependency-tracking
--with-system-zlib --libexecdir=/usr/lib --without-included-gettext --libdir=/usr/lib
--enable-libstdcxx-time=yes --with-tune=generic --enable-version-specific-runtime-libs
--with-gxx-include-dir=/usr/include/c++/4.6 --with-as=/usr/bin/x86_64-w64-mingw32-as --with-ld=/usr/bin/x86_64-w64-mingw32-ld
gcc version 4.6.3 (GCC)

As you can see, this compiler was built with --build=x86_64-linux-gnu and --target=x86_64-w64-mingw32. The "target" is what the compiler builds executables for, so that's what you want to use as "host".

For cmake, it's described here.

### #5Sik_the_hedgehog  Crossbones+   -  Reputation: 2245

Like
0Likes
Like

Posted 31 July 2014 - 02:04 PM

EDIT: forget everything I wrote before (check the edit history if you're curious), turns out it wasn't doing what I thought it was. When I figure out it all I'll post the details of what to do. At least it does seem like I could build the libraries (but installing them is a whole different issue).

Edited by Sik_the_hedgehog, 01 August 2014 - 03:38 AM.

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

### #6Sik_the_hedgehog  Crossbones+   -  Reputation: 2245

Like
3Likes
Like

Posted 04 August 2014 - 12:07 AM

Finally got the whole thing to build! ^o^ Had to reverse engineer configure scripts and makefiles like crazy (Google couldn't have been more helpless), but I figured it out after all. Going to post here what I did in the end. Note: this is for 32-bit Windows, for 64-bit Windows replace i686-w64-mingw32 with x86_64-w64-mingw32 (should be the same for everything else otherwise). Also I'm making the code not use the shared libgcc (in fact it seems the DLL is not present in my system...).

First of all, for the configure-based libraries (except zlib - so SDL2, libogg, libvorbis), this will do:

./configure --build=x86_64-linux-gnu --host=i686-w64-mingw32 --prefix=/usr/local/cross-tools/i686-w64-mingw32 --disable-shared
make
sudo make install

This alone won't work with libvorbis since it'll try to link to libogg from a different path (and will end up trying to link to the Linux libogg by default, whoops!). We have to override this by setting these variables before doing the steps above:

export OGG_CFLAGS="-I/usr/local/cross-tools/i686-w64-mingw32/include"
export OGG_LDFLAGS="-L/usr/local/cross-tools/i686-w64-mingw32/lib -logg"

Then let's go for zlib, which handles Windows in an unique way (and isn't that crosscompile friendly anyway). You will need to do this:

cp win32/Makefile.gcc Makefile
make PREFIX=i686-w64-mingw32-
export BINARY_PATH=/bin
export INCLUDE_PATH=/include
export LIBRARY_PATH=/lib
sudo make install DESTDIR=/usr/local/cross-tools/i686-w64-mingw32

Now for the CMake-based libraries (PhysicsFS, libpng). First we need a file with the following:

SET(CMAKE_SYSTEM_NAME Windows)
SET(CMAKE_C_COMPILER i686-w64-mingw32-gcc)
SET(CMAKE_CXX_COMPILER i686-w64-mingw32-g++)
SET(CMAKE_RC_COMPILER i686-w64-mingw32-windres)
SET(CMAKE_FIND_ROOT_PATH /usr/i686-w64-mingw32)
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

Then we can proceed to build by doing the following (replace ~/wincmake.cmake with whatever is the filename of that file I just mentioned):

mkdir build
cd build
make
sudo make install

Note that the above won't be enough with libpng (I built libpng with cmake since I was making a script and it autodetected cmake for it). You will need to add the following to the cmake line for libpng:

-DZLIB_INCLUDE_DIR=/usr/local/cross-tools/i686-w64-mingw32/include -DZLIB_LIBRARY=/usr/local/cross-tools/i686-w64-mingw32/lib/libz.a

Now to set up Code::Blocks. Go to the compiler settings and make a copy of the GCC compiler (give it whatever name you want, I just appended "(Windows)" to the name). Then go to the search directories path. In the Compiler and Resource compiler tabs (yeah, do the same on both), add this:

/usr/local/cross-tools/i686-w64-mingw32/include
/usr/i686-w64-mingw32/include

/usr/local/cross-tools/i686-w64-mingw32/lib
/usr/i686-w64-mingw32/lib

Then go to the toolchain executables tab and prepend i686-w64-mingw32- to every executable except make, and set the resource compiler to i686-w64-mingw32-windres. No, you shouldn't change the directory, the MinGW-w64 executables get installed to /usr/bin like the rest of the programs.

That's all. Now go make a new Code::Blocks project that's identical to your Linux one but you choose the Windows compiler instead. Add -static-libgcc to the compiler options, and in the build target tabs in the project properties make sure to choose different paths (e.g. wobj/ instead of obj/). Finally, make sure to append .exe to the executable filenames (disable "auto-generate filename extension" or Code::Blocks will get rid of it). Now build! ^o^

• As you can imagine, the program will want to use the DLLs of the libraries. They're located in /usr/local/cross-tools/i686-w64-mingw32/bin, just retrieve them from there and put them in the directory where the EXE resides.
• If you want to make sure the generated executable is indeed a Windows one, use the file command. It should say something along the lines of "PE32 executable (console) Intel 80386 (stripped to external PDB), for MS Windows". If it says ELF, you built a Linux executable instead and you should recheck the Code::Blocks settings.
• When I tried running the game under Wine it crashed in the most epic way possible. It runs flawlessly on the real thing though. Seems like the way SDL2 initializes the audio is making Wine fail horribly. So yeah, don't expect being able to test SDL2 Windows programs on Wine.

Happy crosscompiling!