如何在Linux下交叉编译C++程序生成Windows可执行文件及优势解析
Hey there! Let me walk you through how to build Windows-compatible .exe files from C++ code on a Linux system, plus the big perks of using this cross-compilation approach instead of compiling directly on Windows.
Step-by-Step Cross-Compilation Guide
1. Install the Cross-Compilation Toolchain
The go-to tool for this job is mingw-w64—it’s a GCC-based toolchain that lets you target Windows from Linux. Install it via your distro’s package manager:
- For Debian/Ubuntu-based systems:
sudo apt update && sudo apt install mingw-w64 - For Fedora/RHEL-based systems:
sudo dnf install mingw64-gcc-c++ mingw32-gcc-c++
Note: mingw64 targets 64-bit Windows, while mingw32 handles 32-bit builds. Grab whichever you need (or both, no harm done).
2. Compile a Single C++ File
If you’ve got a simple main.cpp file, generating a Windows .exe is straightforward. Use the cross-compiler variant instead of your regular g++:
- For 64-bit Windows:
x86_64-w64-mingw32-g++ main.cpp -o main.exe - For 32-bit Windows:
i686-w64-mingw32-g++ main.cpp -o main.exe
That’s it—you’ll have a main.exe that runs natively on Windows.
3. Build Complex Projects with CMake
If your project uses CMake (super common for larger codebases), you just need to set up a toolchain file to tell CMake to target Windows:
- Create a file named
mingw-toolchain.cmakewith this content:
set(CMAKE_SYSTEM_NAME Windows) set(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc) set(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++) set(CMAKE_RC_COMPILER x86_64-w64-mingw32-windres) # Handles Windows resource files set(CMAKE_FIND_ROOT_PATH /usr/x86_64-w64-mingw32)
- Run CMake and build as usual, pointing to the toolchain file:
mkdir build && cd build cmake .. -DCMAKE_TOOLCHAIN_FILE=../mingw-toolchain.cmake make
The output will be Windows-ready .exe files in your build directory.
Advantages Over Direct Windows Compilation
- Stick to Your Comfort Zone: If you live and breathe Linux for development, you don’t have to switch to Windows. Keep using your favorite terminal, editor (Vim/VS Code/emacs), and workflow tools—no context switching required.
- Better Performance & Stability: Linux servers (or even desktop Linux) often handle resource-heavy builds better than Windows. For large projects that take hours to compile, Linux’s stability means fewer crashes or interruptions.
- CI/CD Friendly: Linux is the standard for CI/CD pipelines (think GitHub Actions, GitLab CI). Cross-compiling on Linux lets you integrate Windows build jobs into your existing pipeline without maintaining a separate Windows build environment—saves tons of setup time and consistency headaches.
- Simpler Dependency Management: Linux’s package managers (apt, dnf) make grabbing compile-time dependencies a breeze. Many open-source libraries have pre-built packages for mingw-w64, so you don’t have to hunt down Windows-specific binaries or mess with complex setup tools like vcpkg for every dependency.
- Avoid Windows Quirks: No more fighting with backslashes in paths, permission issues, or compiler inconsistencies between different Visual Studio versions. Linux’s consistent environment reduces the "it works on my machine" problems when targeting Windows.
内容的提问来源于stack exchange,提问作者Vinie

