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
Creating executable for Windows using Qt on Linux
Is it possible to create an executable on Linux for both Linux and Windows using the same Qt code with Eclipse? Or is it necessary to install Qt Creator on my Linux machine?
5 Answers 5
If you want to build a windows binary on linux you need to cross-compile. This means you need to have a windows cross-compiler installed plus the libraries you are linking with built with the cross compiler. For a basic Qt program this means you need at least a cross-compiled Qt.
Cross-compiling has nothing to do with Eclipse or Qt Creator. I don’t think both support cross compiling out of the box but I guess you could make them to do so.
Actually i need to create Windows executable in Linux using QT, and my requirement says that i need to install just the Windows QT binary and run the executable which i create in Linux. So please tel me can i create both Linux and Windows executable using QT Eclipse integration in Linux.
I’m able to get Linux executable for my program, but how to get windows executable for the same in Linux?
Sorry, but your question is unclear to me. If you want to cross-compile a Windows binary on Linux you need to cross-compile Qt first, and you need to do that yourself. I haven’t seen any pre-packaged cross-compiled Qt for any OS. Qt Eclipse integration doesn’t create any binaries at all. It runs the compiler, and to build Windows binaries on Linux you need a cross-compiler installed
No Vincat, your linux executable will not run in Windows. You need to do what bluebrother suggested: you need to cross-compile it for Windows. That means you install on your linux machine a cross-compiling package that will compile your code for Windows and link against the Windows libraries. Then you can use THAT executable on Windows. Look into the MinGW compiler, I know it compiles Qt on Windows and has cross-compilers available for linux.
Is it possible to create a Windows exe on Linux using C++ with CMake?
I’m currently working from my home Windows PC on the code, that I compile on a Linux PC over an SSH connection. But since the resulting program creates a GUI, I can’t really test it. Is it possible to create, besides the Linux program, a Windows exe that I can run on my home computer? Here is my CMakeLists (using QT5):
cmake_minimum_required(VERSION 2.8.11) project(p3) set(CMAKE_PREFIX_PATH $ENV) MESSAGE(STATUS "QT5_QMAKE_DIR: " $) set(EXECUTABLE_OUTPUT_PATH $/bin) set(LIBRARY_OUTPUT_PATH $/lib) set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_VERBOSE_MAKEFILE on) set(CMAKE_AUTOMOC ON) # Possibly set this to DEBUG for testing set(CMAKE_BUILD_TYPE Release) include_directories($) #project source directories find_package(Qt5Widgets REQUIRED) find_package(Qt5Core REQUIRED) find_package(Qt5Gui REQUIRED) find_package(OpenMP) if (OPENMP_FOUND) message("OPENMP FOUND") set(CMAKE_C_FLAGS "$ $") set(CMAKE_CXX_FLAGS "$ $") set(CMAKE_EXE_LINKER_FLAGS "$ $") endif() include_directories( $ $ $ ) add_definitions($) set(CMAKE_CXX_FLAGS "$ $") if(UNIX) set(CMAKE_CXX_FLAGS "$ -std=c++11") endif() add_executable(p3 #.cpps and .hs ) set_property(TARGET p3 PROPERTY CXX_STANDARD 11) target_link_libraries(p3 $)
Here’s a question that addresses compiling for Windows on Linux; the accepted answer uses mingw, and it doesn’t directly address cmake, but give it a look: stackoverflow.com/questions/2033997/…
Alexander’s suggestion using X11 forwarding is what I use to display Linux GUI on Windows, only I use Putty and Xming instead of MobaXterm. I may download it and try it out.
I have used Putty and Xming for a while. It was a hard time :). Seriously, they are a bit harder to set up.
Compile Windows C console applications in Linux
Best answer so far, and yes, there was a dupe 😀 Also nice to post the needed packages. Straight answer, to the punch.
Thanks for the answer. Also sorry for the duplicate question, I didn’t spot it. 🙂 Keep up the good work! Thanks again.
On Fedora, we also have resources for learning about compiling for win32 using mingw, an irc channel, the mingw32 utilities and a bunch of pre-built libraries for windows — for more info, see: fedoraproject.org/wiki/SIGs/MinGW .
You can if it’s standard C, and doesn’t use Windows libraries.
C code itself is very portable, and the standard C libraries (libc) are available pretty much everywhere. If your code does printf() and sscanf() and fopen() and so on, then it will just compile and run on another platform. Windows, Linux, BSD, etc.
It’s the libraries other than libc that introduce portability challenges.
Anything that links with Windows-specific platform libraries is trouble. Kernel32.lib, user32.lib, etc etc.
There are third-party libs, too, that, if written in C, should be available across Linux and Windows. PCRE is a good example here — it’s a Regular Expression library written in C, and it’s available on Windows as well as on Linux. there are literally hundreds of libraries in this set.
If you confine yourself to libc and library calls into portable libs, then you will have a portable C application.