How to compile for Windows on Linux with gcc/g++?
So I was wondering if it is possible to have g++ make static compiled Windows executables that contains everything needed? I don’t have Windows, so it would be really cool, if I could do that on Linux 🙂
@AndiDog, «First dose for free», right. Anyway, setting up automated build process on Windows machine, while you have a completed and working one for Linux, is unnecessary.
@el.pescado, building and testing are completely different tasks. Windows is unnecessary for the former.
7 Answers 7
mingw32 exists as a package for Linux. You can cross-compile and -link Windows applications with it. There’s a tutorial here at the Code::Blocks forum. Mind that the command changes to x86_64-w64-mingw32-gcc-win32 , for example.
Ubuntu, for example, has MinGW in its repositories:
$ apt-cache search mingw [. ] g++-mingw-w64 - GNU C++ compiler for MinGW-w64 gcc-mingw-w64 - GNU C compiler for MinGW-w64 mingw-w64 - Development environment targeting 32- and 64-bit Windows [. ]
If you use debian, mingw32 is already in the repository, together with few precompiled libraries too.
Well, there’s a cross-compilation environment at nongnu.org/mingw-cross-env. It includes freeglut, for example. But I haven’t used this, so don’t ask me about it 😉
Does the «32» in mingw32 mean that I can only produce a 32-bit binary? Is there a solution to produce a 64-bit binary as well?
bluenote10: there are variants that build for x64, such as «x86_64-w64-mingw32-g++». The base «mingw32» may or may not be capable, but it’s easy enough to install/use the variants by name. ar2015: Does it not support C++11 at all or are you talking about a problem you had with it? I’m working on getting a project to build with mingw as we speak and this would be good information to know. Other threads indicate that it does support c++11 (e.g. stackoverflow.com/questions/16136142/…). Of course I’d be happy to help, but a separate post would be best for that.
Suggested method gave me error on Ubuntu 16.04: E: Unable to locate package mingw32
To install this package on Ubuntu please use following:
sudo apt-get install mingw-w64
After install you can use it:
For 64-bit use: x86_64-w64-mingw32-g++
For 32-bit use: i686-w64-mingw32-g++
I tried. It just throws all sort of errors regarding DWORD, LPVOID, WORD, BYTE and so on. Isn’t doing anything cross-compilation
@RichardMcFriendOluwamuyiwa Looks like you missed some headers to compile it on linux. It could generate binaries for running on Win machine. Please update your sources.
I made it work by including the windows.h before the winbase.h header. VS code was automatically rearranging the includes and leading to error so I had to force the order by empty comment lines
One option of compiling for Windows in Linux is via mingw. I found a very helpful tutorial here.
To install mingw32 on Debian based systems, run the following command:
sudo apt-get install mingw32
To compile your code, you can use something like:
i586-mingw32msvc-g++ -o myApp.exe myApp.cpp
You’ll sometimes want to test the new Windows application directly in Linux. You can use wine for that, although you should always keep in mind that wine could have bugs. This means that you might not be sure that a bug is in wine, your program, or both, so only use wine for general testing.
To install wine, run:
sudo apt-get install wine
Building a Cross compiler for Windows on Linux
This is a small How-To on creating a cross compiler for Windows that runs on Linux.
Intro
Why would one ever do something as stupid as compiling Qt/Windows applications on Linux? There are several (good and better) reasons to do this:
- You don’t feel comfortable working under Windows.
- The shell on Linux is far more powerful than Windows own cmd.exe.
- You don’t want to reboot your workstation just to do another compile.
- You don’t want to transport your working copy of the source tree on a floppy or memory stick, just because Windows is too dumb to read most Linux file systems.
This article will show you how to create an environment on Linux, so that you don’t need to leave it for compiling Windows applications. Unfortunately you’ll still need Windows to test these applications.
Getting the sources
If you installed Qt/Win32 with MinGW before, just copy the MinGW source files (c:\MinGW\src\*.gz) from Windows to Linux.
Otherwise download the source packages from: http://mingw.sourceforge.net/download.shtml, you’ll need these packages (the versions in parenteses are the current ones I tested with, newer ones should work as well):
- binutils (2.15.94)
- gcc-core (3.4.2)
- gcc-g++ (3.4.2, must match gcc-core!)
- mingw-runtime (3.7)
- w32api (3.2)
Creating the MinGW cross-compiler
It is assumed that:
TARGET=i586-mingw32
PREFIX=$HOME/mingw- Unpack all archives
- compile binutils:
./configure --target=$TARGET --prefix=$PREFIX
make && make install - Copy the header files to the target:
mkdir -vp $PREFIX/include
cp -r mingw-runtime-*/include/* $PREFIX/include
cp -r w32api-*/include/* $PREFIX/include - Compile GCC for C only (you need C for the libs and the libs for C++):
./configure --prefix=$PREFIX --target=$TARGET --enable-threads --enable-languages=c
make && make install - Compile the Windows API:
./configure --prefix=$PREFIX --target=$TARGET --host=$TARGET --build=$(./config.guess)
make && make install - Compile the Windows Runtime:
mv w32api-* w32api
cd mingw-runtime*
./configure --prefix=$PREFIX --target=$TARGET --host=$TARGET --build=$(./config.guess)
make && make install - Re-compile GCC for C & C++:
./configure --prefix=$PREFIX --target=$TARGET --enable-threads --enable-languages=c,c++
make && make install
It might happen that GCC has problems with linking, the solution is easy:
The install scripts create $PREFIX/lib and $PREFIX/$TARGET/lib — move everything from $PREFIX/$TARGET/lib to $PREFIX/lib and then make the former a link to the latter:
cd $PREFIX/$TARGET
rmdir lib
ln -s ../lib libHow to proceed
From there on you can use the MinGW C/C++ compiler just like any other version of GCC.
In Part 2 of this article I will explain how to cross compile Qt4/Win32.
In Part 3 I will explain how to compile Qt applications using this cross compiler.
How to install mingw32 on Ubuntu?
I would like to install mingw32 on my Ubuntu (16.10) cuz I would like to generate an x86 windows version of my application. The «i586-mingw32msvc-g++» executable is needed by my Makefile (CMake). I tried «sudo apt install mingw32» but it doesn’t work (I have a «package not found» error).
3 Answers 3
Look more closely at what is actually available:
edd@max:~$ apt-cache search mingw- libassuan-mingw-w64-dev - IPC library for the GnuPG components -- Windows port libgcrypt-mingw-w64-dev - LGPL Crypto library - Windows development libgpg-error-mingw-w64-dev - library of error values and messages in GnuPG (Windows development) libksba-mingw-w64-dev - X.509 and CMS support library (Windows development) libnpth-mingw-w64-dev - replacement for GNU Pth using system threads (Windows dev) binutils-mingw-w64 - Cross-binutils for Win32 and Win64 using MinGW-w64 binutils-mingw-w64-i686 - Cross-binutils for Win32 (x86) using MinGW-w64 binutils-mingw-w64-x86-64 - Cross-binutils for Win64 (x64) using MinGW-w64 g++-mingw-w64 - GNU C++ compiler for MinGW-w64 g++-mingw-w64-i686 - GNU C++ compiler for MinGW-w64 targeting Win32 g++-mingw-w64-x86-64 - GNU C++ compiler for MinGW-w64 targeting Win64 gcc-mingw-w64 - GNU C compiler for MinGW-w64 gcc-mingw-w64-base - GNU Compiler Collection for MinGW-w64 (base package) gcc-mingw-w64-i686 - GNU C compiler for MinGW-w64 targeting Win32 gcc-mingw-w64-x86-64 - GNU C compiler for MinGW-w64 targeting Win64 gdb-mingw-w64 - Cross-debugger for Win32 and Win64 using MinGW-w64 gdb-mingw-w64-target - Cross-debugger server for Win32 and Win64 using MinGW-w64 gfortran-mingw-w64 - GNU Fortran compiler for MinGW-w64 gfortran-mingw-w64-i686 - GNU Fortran compiler for MinGW-w64 targeting Win32 gfortran-mingw-w64-x86-64 - GNU Fortran compiler for MinGW-w64 targeting Win64 gnat-mingw-w64 - GNU Ada compiler for MinGW-w64 gnat-mingw-w64-base - GNU Ada compiler for MinGW-w64 (base package) gnat-mingw-w64-i686 - GNU Ada compiler for MinGW-w64 targeting Win32 gnat-mingw-w64-x86-64 - GNU Ada compiler for MinGW-w64 targeting Win64 gobjc++-mingw-w64 - GNU Objective-C++ compiler for MinGW-w64 gobjc++-mingw-w64-i686 - GNU Objective-C++ compiler for MinGW-w64 targeting Win32 gobjc++-mingw-w64-x86-64 - GNU Objective-C++ compiler for MinGW-w64 targeting Win64 gobjc-mingw-w64 - GNU Objective-C compiler for MinGW-w64 gobjc-mingw-w64-i686 - GNU Objective-C compiler for MinGW-w64 targeting Win32 gobjc-mingw-w64-x86-64 - GNU Objective-C compiler for MinGW-w64 targeting Win64 libz-mingw-w64 - compression library - Windows runtime libz-mingw-w64-dev - compression library - Windows development files mingw-ocaml - ocaml-mingw-w64 transitional dummy package mingw-w64 - Development environment targeting 32- and 64-bit Windows mingw-w64-common - Common files for Mingw-w64 mingw-w64-i686-dev - Development files for MinGW-w64 targeting Win32 mingw-w64-tools - Development tools for 32- and 64-bit Windows mingw-w64-x86-64-dev - Development files for MinGW-w64 targeting Win64 ocaml-mingw-w64 - OCaml cross-compiler based on mingw -- Meta-package ocaml-mingw-w64-i686 - OCaml cross-compiler based on mingw -- 32 bit compiler ocaml-mingw-w64-x86-64 - OCaml cross-compiler based on mingw -- 64 bit compiler edd@max:~$
So sudo apt-get install gcc-mingw-w64 is probably what you want, and you should get a 32-bit executable built by setting the appropriate compiler option, likely -m32 .