How to compile for Windows on Linux with gcc/g++?
Asked Answered
F

7

180

I have written some effects in C++ (g++) using freeglut on Linux, and I compile them with

g++ -Wall -lglut part8.cpp -o part8

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 :)

Fisc answered 9/1, 2010 at 16:27 Comment(4)
@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.Shrine
How would you test your Windows software without Windows? Using Wine?Calcimine
@el.pescado, building and testing are completely different tasks. Windows is unnecessary for the former.Shrine
Possible duplicate of Is it possible to compile Windows binaries on a linux machine?Hydrodynamic
D
142

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
[...]
Diedrediefenbaker answered 9/1, 2010 at 16:31 Comment(10)
If you use debian, mingw32 is already in the repository, together with few precompiled libraries too.Nonviolence
Yes just added it to my post as you wrote your comment :) ThxDiedrediefenbaker
to be complete, you'd need to provide a mingw-compatible glut library too, no ?Sagittate
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 ;)Diedrediefenbaker
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?Essene
It has problem with C++11Zohar
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. #16136642). Of course I'd be happy to help, but a separate post would be best for that.Melany
Are things on this topics changed recently?Alexandrite
The link is from 17 to 7 years old... Need a refresh!Scenery
what about c++17 or gcc 9 ? I searched everywhere, but didn’t understand what was wrong with that?Amathiste
V
51

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:

x86_64-w64-mingw32-g++

Please note!

For 64-bit use: x86_64-w64-mingw32-g++

For 32-bit use: i686-w64-mingw32-g++

Vladamir answered 1/11, 2017 at 18:9 Comment(3)
I tried. It just throws all sort of errors regarding DWORD, LPVOID, WORD, BYTE and so on. Isn't doing anything cross-compilationMandatory
@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.Vladamir
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 linesMandatory
B
39

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

Byzantium answered 19/7, 2013 at 7:30 Comment(3)
you do not need to specify mingw32-binutils and mingw32-runtime because they are dependencies of mingw32Counterespionage
cool, the cpp had very simple errors (void main(){} etc...), I guess it compiles at windows but fails at mingw, I fixed them. I tried also i586-mingw32msvc-cpp and it generated a text file with something like things to be compiled lol.. the g++ one worked, thx!Humane
@Counterespionage to be thorough, if you don't specify them, they can be removed automatically by the package manager when uninstalling mingw32 (marked as "dependencies". I you do specify them, they won't (marked as "manual").Foregather
E
19

Install a cross compiler, like mingw64 from your package manager. Then compile in the following way: instead of simply calling gcc call i686-w64-mingw32-gcc for 32-bit Windows or x86_64-w64-mingw32-gcc" for 64-bit Windows. I would also use the --static option, as the target system may not have all the libraries.

If you want to compile other language, like Fortran, replace -gcc with -gfortran in the previous commands.

Estus answered 13/1, 2016 at 19:1 Comment(0)
W
6

I've used mingw on Linux to make Windows executables in C, I suspect C++ would work as well.

I have a project, ELLCC, that packages clang and other things as a cross compiler tool chain. I use it to compile clang (C++), binutils, and GDB for Windows. Follow the download link at ellcc.org for pre-compiled binaries for several Linux hosts.

Weed answered 9/1, 2010 at 16:31 Comment(1)
Since I posted that answer, I've gone a bit further. I have a project, ELLCC, that packages clang and other things as a cross compiler tool chain. I use it to compile clang (C++), binutils, and GDB for Windows. Follow the download link at ellcc.org for pre-compiled binaries for several Linux hosts.Weed
T
4

From: https://fedoraproject.org/wiki/MinGW/Tutorial

As of Fedora 17 it is possible to easily build (cross-compile) binaries for the win32 and win64 targets. This is realized using the mingw-w64 toolchain: http://mingw-w64.sf.net/. Using this toolchain allows you to build binaries for the following programming languages: C, C++, Objective-C, Objective-C++ and Fortran.

"Tips and tricks for using the Windows cross-compiler": https://fedoraproject.org/wiki/MinGW/Tips

Threemaster answered 23/2, 2014 at 3:37 Comment(0)
S
4

For Fedora:

# Fedora 18 or greater
sudo dnf group install "MinGW cross-compiler"

# Or (not recommended, because of its deprecation)
sudo yum groupinstall -y "MinGW cross-compiler"
Scarito answered 20/10, 2014 at 14:0 Comment(2)
For all those Fedora users: there is a great tutorial here: fedoraproject.org/wiki/MinGW (edited because wrong link)Quitt
Exactly what I was looking for! Works with sudo dnf groupinstall ... too.Catamount

© 2022 - 2024 — McMap. All rights reserved.