Beginner guide
In about ten minutes you write a five-line C program on Linux, build it into a Windows .exe with WinBridge, and run that .exe without touching a Windows machine.
What cross-compiling is
A compiler turns source code into a program for one kind of system. Normally it builds for the system it runs on: compile on Linux, get a Linux program.
Cross-compiling means building on one system for a different one. Here the compiler runs on Linux but writes a Windows program: a file in the PE format that Windows uses, usually ending in .exe. Linux cannot start that file directly, and Windows cannot start a Linux program.
Each language has its own way to cross-compile, with its own flags and extra tools. WinBridge hides those differences: it detects the project type, calls the right compiler with the right settings, and checks that the output really is a Windows executable.
What you need
- WinBridge installed. See Install.
- An internet connection for two downloads: the Zig compiler (56 MB) and, in the last part, the Wine runtime (about 94 MB).
You do not need a C compiler installed. WinBridge fetches one into your home folder.
Build your first Windows program
See what WinBridge can use on your machine.
bashwinbridge doctorLook at the Installable components section. It lists what WinBridge can download for you and how large each download is. Under Toolchains, a line starting with
MISSINGnames a tool that a toolchain needs and that is not installed.Install the Zig compiler.
bashwinbridge doctor --install zigWinBridge uses Zig as its C and C++ cross-compiler. It is downloaded, checked against a checksum, and unpacked into
~/.cache/winbridge/toolchains/. Nosudois involved.Create a project folder with one source file.
bashmkdir hello cd helloSave this as
main.cin thehellofolder:c#include <stdio.h> int main(void) { printf("hello from winbridge\n"); return 0; }Ask WinBridge what it sees.
bashwinbridge detectThe answer names the project (
hello, after the folder), its type (C/C++), the file that decided it (main.c) and the toolchain that would build it (c). Nothing is built yet.Build it.
bashwinbridge buildWinBridge prints
Building hello (C/C++) for windows-x64 with c, runs the compiler, checks the result, and ends with a line likeBuilt hello for windows-x64followed by the path and size of the file it made.Look at the result.
bashls build/winbridge/windows-x64/You see
hello.exe. This is a 64-bit Windows program. You can copy it to a Windows PC and run it there.
Good to know
Build output always goes to build/winbridge/windows-x64/ inside the project folder, unless you pass --output. Compiler caches stay out of your project, under ~/.cache/winbridge/build/.
Run it on Linux
Start the program with
winbridge run.bashwinbridge run build/winbridge/windows-x64/hello.exeWait for the first-run setup.
The first time, WinBridge downloads its Wine runtime into
~/.cache/winbridge/runtimes/and printsCreating a new prefix for hello (first run takes a moment). Wine is a compatibility layer that lets Linux run Windows programs. A prefix is a private Windows environment for one app, with its own C: drive and registry.Read the output.
The program prints:
texthello from winbridgeRun the same command again. This time it starts straight away, because the runtime and the prefix already exist.
Clean up
The prefix that winbridge run created is named after the executable. List what exists, then delete the one from this guide:
winbridge prefix list
winbridge prefix delete helloDeleting a prefix removes everything in that app's Windows environment. For this test program that is nothing you need.
What you learned
winbridge doctorshows which toolchains are ready, andwinbridge doctor --installfetches a compiler withoutsudo.winbridge detectreports the project type without building.winbridge buildproduces a verified Windows.exeinbuild/winbridge/windows-x64/.winbridge runstarts a Windows program on Linux in its own prefix.