How to compile for Windows on Linux with gcc/g++?
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
[...]
Is it possible to compile from Windows to Linux with gcc/g++?
You have several choices:
- WSL.
WSL(Windows Subsystem for Linux) its linux termanal in windows, so you can compile linux code in windows. This solution is the simpliest and I would recommend to use it. - Visual studio.
Visual studio has a package that allows you to compile programs for Linux. More details here
How to compile executable for Windows with GCC with Linux Subsystem?
Linux Subsystem works as a Linux-computer. You can only run Linux executables inside it and default gcc
creates Linux executables.
To create Windows executables, you need to install mingw cross-compiler:
sudo apt-get install mingw-w64
Then you can create 32-bit Windows executable with:
i686-w64-mingw32-gcc -o main32.exe main.c
And 64-bit Windows executable with:
x86_64-w64-mingw32-gcc -o main64.exe main.c
Note that these Windows executables will not work inside Linux Subsystem, only outside of it.
Cross compile modern C++ from Linux to Windows
My Ubuntu 18.04 has a POSIX-threaded mingw-w64 toolchain as well. Try update-alternatives --list x86_64-w64-mingw32-g++
.
How to compile C code in Linux to run on Windows?
You would need a cross-compiler to create a Windows executable in Linux.
Mingw-w64 is an advancement of the original mingw.org project, created to support the GCC compiler on Windows systems.
Installing the cross-compilation
sudo apt-get install mingw-w64
32bit
i686-w64-mingw32-gcc -o test.exe test.c
64bit
x86_64-w64-mingw32-gcc -o test.exe test.c
(C++) How do I use libraries when compiling for windows on linux?
What you are looking for is the M Cross Environnement: https://mxe.cc/
It is a set of makefiles able to download and cross compile a selection of popular libraries for windows on linux. By default it builds static libraries, such that you ends up with .a libraries that get merged into the final .exe, meaning you don't have to worry about shipping the dlls with your app. But if you prefer a modular structure, it can also builds some dlls.
The list of libraries they do provide is quite impressive. If a library is missing on the other hand you can still install it by copying the corresponding header files and dlls. In some specific situation you might have to cross-compile one of your dependency (I had to do that for an app using ruby scripting. The official windows build of ruby is somehow incompatible with certain libraries built with mingw. But this is rather exceptional).
Compile file in linux and run in windows
You can use a mingw cross compiler to build windows binaries in linux. In Ubuntu (and I guess other Debian variants as well) the package is called mingw32
. You then have a cross compiler under the name i586-mingw32msvc-g++
(or similar). For building simple command line programs without library dependencies this is an OK solution.
If you need more then this I'd recommend you use MXE (M cross environment). MXE installs its own cross compiler and can build many libraries for you so you don't need to care about how to build the library dependencies.
For example the OpenSCAD project (a 3D CAD program that is using Qt for its GUI) is using MXE for building the Windows releases. See this page on the OpenSCAD wiki for a description of the build process.
Related Topics
What How to Use Instead of the Arrow Operator, '-≫'
Is the Safe-Bool Idiom Obsolete in C++11
Executing Cv::Warpperspective For a Fake Deskewing on a Set of Cv::Point
C++ Virtual Function Return Type
Remove Comments from C/C++ Code
Forward Declaration of a Typedef in C++
Why Doesn't Std::Queue::Pop Return Value.
Why No Default Move-Assignment/Move-Constructor
Is Stateful Metaprogramming Ill-Formed (Yet)
Export All Symbols When Creating a Dll
What's the Difference Between a Header File and a Library
What Is the Fastest Way to Transpose a Matrix in C++
How to Read a Complete Line from the User Using Cin
How Do Promotion Rules Work When the Signedness on Either Side of a Binary Operator Differ