Cross-compiling on Windows and Linux
I am developing an application that will have to run on both Windows and Linux. So far I have it running on Linux using GCC 4.4.1 using my Makefile. However, I also need to compile on Windows. The source code will compile on Windows as I have #defined all the areas of the code that separate the different compilers. I.e.:
#ifdefined (LINUX) /* Do linux stuff */ #else /* Do windows stuff */ #endif
And so far the code is very simple as I am just starting this program. Basically I just want to test my idea. However, is it as simple as compiling the source code on Linux? Then when I want to compile on to Windows I could copy the files to the Windows platform. Then opening the source code files in Visual Studio C++ and creating a project and then compiling to create my binary?
7 Answers 7
Take a look at CMake as well.
Using Visual C++ is one way of building your C project on Windows. It’s not the only way, though.
VC has project files that you will need to create. But then realize that you’ll have to maintain the build configuration in two different places (make & .vcproj).
There are other options as well:
- You can also use make on Windows (e.g. on Cygwin)
- There’s nmake, msbuild, etc.
- You could also use ant (which will work on both platforms).
If I were you I’d try to use as few different build tools as possible.
You may be able to use MinGW to cross-compile Windows binaries directly from Linux. Most of the popular Linux distros now have MinGW packages available that make this very straightforward.
Then you can build all your Windows and Linux binaries in one go, on one machine, from one Makefile. No need to mess around trying to get GNU Make to work properly in Windows, let alone worry about Visual Studio.
SCons is an excellent cross-platform build system. Instead of writing a Makefile , you write a SConstruct file, which is usually simpler and yet more powerful. The scons command will (by default) build for the operating system on which it’s run, providing an easy way to manage cross-platform builds.
For simple console applications (and even more complex ones, like the Qt framework) you can build on Windows using MinGW. Building with MinGW is pretty similar to building on Linux, and it isn’t unlikely that you can use the same Makefile with no or only minor modifications. This has the major advantage that you don’t need to maintain two sets of Makefiles / project files.
You can also use Cygwin, which basically is an emulation layer of the Linux API for the substantial functionality. Using Cygwin however has the disadvantage that it links to its own library cygwin1.dll . This might not be an issue for you as you can simply ship this DLL with your program. You can also suppress using this by using the option -mno-cygwin as compiler option for gcc / g++ . However, for some functionality the cygwin1.dll functionality is required.
I can recommend MinGW for building programs on Windows that have originally been written for Linux. I’m using this for various command line programs, and for GUI applications I’m using the Qt framework with the MinGW compiler. If you want to build GUI applications you should definitely use a platform independent GUI framework as it makes things much easier.
Manual for cross-compiling a C++ application from Linux to Windows?
Is there a manual for cross-compiling a C++ application from Linux to Windows? Just that. I would like some information (links, reference, examples. ) to guide me to do that. I don’t even know if it’s possible. My objective is to compile a program in Linux and get a .exe file that I can run under Windows.
3 Answers 3
The basics are not too difficult:
sudo apt-get install mingw32 cat > main.c EOF i586-mingw32msvc-cc main.c -o hello.exe
Replace apt-get with yum , or whatever your Linux distro uses. That will generate a hello.exe for Windows.
Once you get your head around that, you could use autotools, and set CC=i586-mingw32msvc-cc
CC=i586-mingw32msvc-cc ./configure && make
Or use CMake and a toolchain file to manage the build. More difficult still is adding native cross libraries. Usually they are stored in /usr/cross/i586-mingw32msvc/ and you would need to add those paths in separately in the configure step of the build process.
One thing to note with this is that some libraries don’t like to cross-compile. If your only choices are an .exe installer and source, you may want to install the library under wine and then copy the libraries and headers into your mingw search path. I never could get Boost to cross-compile
WINE is okay for stupid-simple programs like this, but a VM (VirtualBox, VMWare, QEMU, etc) is your best bet.
If you want to generate a 64-bit Windows binary, install the 64-bit tools: $ sudo apt-get install mingw-w64 . The compiler is x86_64-w64-mingw32-gcc .
It depends on what you mean (I couldn’t really say).
- If you mean that you want to use an existing Linux application on Windows, then you could try compiling it using Cygwin on Windows. This however does not give you a Windows executable free from all dependencies towards Cygwin (your executable still depends on the cygwin.dll file) — and it still may need some porting before it will work. See http://www.cygwin.com.
- If you mean that you want to be able to perform the actual compilation of a Windows application on Linux and produce a .exe file that is executable on Windows — thus using your Linux box for development and/or compilation then you should look into MinGW for Linux which is a tool for crosscompiling for Windows on Linux. See http://www.mingw.org/wiki/LinuxCrossMinGW.
I suggest you give the following, GUB (Grand Unified Builder) a try as it cross-compiles several packages with their dependencies and assembles them into a single installation package for currently 11 architectures. You can download a prebuilt iso for installation in a VM from here and follow the source here. It can currently be used to cross-compile GNU LilyPond/ GNU Denemo / Inkscape and OpenOffice.org.
The target architectures are:
- darwin-ppc — tar.bz2 file for Darwin 7 (MacOS 10.3)/PowerPC
- darwin-x86 — tar.bz2 file for Darwin 8 (MacOS 10.4)/x86
- mingw — mingw executable for Windows32
- linux-x86 — shar archive for Linux/x86
- linux-64 — shar archive for Linux/x86_64
- linux-ppc — shar archive for Linux/PowerPC
- freebsd-x86 — shar archive for FreeBSD 4/x86
- freebsd-64 — shar archive for FreeBSD 6/x86_64
- cygwin — .tar.bz2 packages for Cygwin/Windows32
- arm — shar archive for Linux/ARM (largely untested)
- debian — shar archive for Debian (largely untested)
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
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