A Comprehensive Guide to Compiling Windows Executables with GCC in Linux Subsystem

Dec 07, 2025 · Programming · 11 views · 7.8

Keywords: GCC | Linux Subsystem | Cross-Compilation

Abstract: This article details how to compile C source code into Windows executables (.exe) by installing the mingw-w64 cross-compiler in the Linux Subsystem on Windows 10. It explains the differences between the Linux subsystem and native Windows environments, provides compilation commands for 32-bit and 64-bit executables, and discusses related considerations.

Introduction

With the introduction of the Linux Subsystem (WSL) in the Windows 10 Anniversary Update, developers can run Linux toolchains, such as the GCC compiler, within the Windows environment. However, many users find that code compiled in the subsystem defaults to generating Linux executables, which cannot run directly on Windows. This article addresses this issue by using cross-compilation techniques to produce Windows-compatible executables.

Linux Subsystem and Compilation Environment

The Linux subsystem is essentially a compatibility layer that allows users to run Linux binaries on Windows. When GCC is installed via sudo apt-get install gcc, the compiler defaults to generating code for the Linux environment. For example, compiling a simple C program:

#include <stdio.h>
int main(void){
    printf("Hello\n");
    return 0;
}

Using the command gcc -c main.c produces an object file main.o, which is a Linux executable. Running ./main.o in the subsystem outputs Hello. However, this file cannot execute in the native Windows environment because it relies on Linux system calls and libraries.

Cross-Compilation Solution

To generate Windows executables, the mingw-w64 cross-compiler must be installed. In the Linux subsystem, execute the following command to install it:

sudo apt-get install mingw-w64

After installation, specific commands can be used to compile C code. For a 32-bit Windows executable, use:

i686-w64-mingw32-gcc -o main32.exe main.c

This command invokes the i686-w64-mingw32-gcc compiler to compile main.c into main32.exe, a 32-bit Windows PE-format executable. Similarly, for a 64-bit Windows executable, use:

x86_64-w64-mingw32-gcc -o main64.exe main.c

Here, x86_64-w64-mingw32-gcc targets the x86-64 architecture to produce a 64-bit executable. Both commands leverage the mingw-w64 toolchain, which provides a compatible implementation of the Windows API, ensuring the code can run on Windows.

Considerations and In-Depth Analysis

The generated .exe files can only run in the native Windows environment, not within the Linux subsystem, as they depend on the Windows kernel and dynamic link libraries. Additionally, cross-compilation may involve library dependency issues; for instance, if the code uses Linux-specific libraries, additional configuration or static linking might be necessary. It is advisable to check the cross-platform compatibility of the code before compilation and use the -static option to avoid dynamic library problems.

From a technical perspective, this process illustrates the core principle of cross-compilation: the compiler generates code for one platform (Windows) on another platform (Linux). Mingw-w64 achieves this by emulating the Windows environment, including headers and library files. In practical applications, this can be used for developing cross-platform software or testing Windows applications.

Conclusion

By installing the mingw-w64 cross-compiler, developers can easily compile C code into Windows executables within the Linux subsystem. This article provides complete steps and commands to help users overcome environmental differences and achieve efficient cross-platform development. In the future, as WSL evolves, more integrated tools may simplify this process.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.