Skip to content
Nim Cross-platform Compile Commands

Nim Cross-platform Compile Commands

Cross-platform compiling

Linux

Mac OS

Windows

Compiling for Windows (Cross-Compiling from Linux)Since you are on Linux, you don’t even need a Windows machine to build the .exe. You can cross-compile directly from your Ubuntu terminal by installing the MingW toolchain:bash# 1. Install the cross-compiler sudo apt install mingw-w64

2. Compile your project into a Windows executable

nim c -d:release –cpu:amd64 –os:windows src/webui_test.nim

Ok. will start to clone the repoI’ll be back18:20Awesome, go ahead and clone the repository!Since your path environment is fully set up, cloning the project onto your Mac and running nimble build -d:release should be a breeze.Take your time getting the files pulled down, and I will be right here waiting for you to return. Let me know how the macOS build turns out!AI-svar kan inneholde feil. Finn ut merIs there any penalty to use nim c -d:release –passC:"-arch arm64 -arch x86_64" –passL:"-arch arm64 -arch x86_64" -o:build/monitor_act_universal src/monitor_act.nim to support both amd64 & arm64?18:58No, there is no performance penalty at runtime for the end-user.Creating a “Universal Binary” (also called a Fat Binary) is the official, Apple-approved way to distribute software that runs natively at maximum speed on both older Intel Macs and modern Apple Silicon Macs.However, there are three minor developer “penalties” or trade-offs to keep in mind:1. Larger File Size (Double the size)The Penalty: The final executable will be roughly twice as large as a single-architecture binary.Why: The compiler literally packs two complete, separate compiled programs (one for arm64 and one for x86_64) into a single file container. When a user runs it, macOS automatically reads the container and executes only the half that matches their machine’s CPU.2. Slower Compilation SpeedThe Penalty: The compilation process will take about twice as long to finish.Why: The Nim compiler has to run the entire backend code generation cycle twice under the hood—once for each target chip architecture.3. Nimble BypassThe Penalty: You have to use the raw nim c command you wrote above instead of the automated nimble build command.Why: Standard Nimble configuration syntax doesn’t have a clean, built-in way to pass multi-architecture flags natively to the linker for a single target, so your manual command is the correct way to build it.VerdictFor a lightweight utility like monitor_act, the file size increase is negligible (it will likely go from ~200KB to ~400KB), and a few extra seconds of build time is totally worth it. It gives you a single, flawless file that you can attach to your Forgejo release that works on any Mac.How did the universal build run on your Mac terminal? Did it generate the monitor_act_universal file without any complaints from the compiler?AI-svar kan inneholde feil. Finn ut merWill try the universial build now on a i9 macWill be back19:01Excellent! An Intel i9 Mac is the perfect place to build it. Since it has a native Intel processor, the compiler will use its local tools to generate both segments without a hitch.Take your time running the universal compilation command. I will be right here waiting to hear how the build turns out and if the final fat binary runs smoothly!AI-svar kan inneholde feil. Finn ut merOk. compiled a universal binary on mac. Works nicely.10 sec compile time, and with one simple command! Excellent!Other modern languages. Take notice!21:26That is absolutely awesome to hear! 10 seconds for a full cross-architecture production binary is an incredible result.You hit the nail on the head—other modern languages really should take notice. Nim’s unique approach of compiling directly down to lightweight, standard C code means it doesn’t need to ship a massive runtime engine or virtual machine. It just lets the native system compiler do what it does best. That is exactly why it is so fast and efficient.Now that you have your monitor_act_universal binary built and tested, you are ready to update your Forgejo server!How to Add the Mac Binary to Your Existing ReleaseYou do not need to create a brand new release for macOS. It is best practice to keep all platform versions of the same code version together under Release v0.1.0.Open your web browser and go to your Forgejo server.Click on the Releases tab.Find your 0.1.0 release and click the Edit button (usually a small pencil icon or button on the right side).Scroll down to the Attach Files box at the bottom.Drag and drop your new monitor_act_universal file right next to your existing Linux binary.Click Save Release (or Update Release).Now, whenever you visit your server’s release page, you will see a clean, professional distribution layout: the Linux binary for your server/workstations, and the universal Mac binary for your laptops!What is next on the roadmap for monitor_act? Would you like to look at adding a new feature, or perhaps setting up some automated testing scripts to keep it stable?AI-svar kan inneholde feil. Finn ut merOk. file uploaded. Suppose it’s ok to re-name monitor_act_universal to monitor_act_mac on the forgejo releases page21:32Yes, renaming the file to monitor_act_mac is a great idea and is considered best practice.When people download tools from a releases page, they expect the file name to clearly tell them which operating system it belongs to. Changing it from monitor_act_universal to monitor_act_mac makes it immediately clear to anyone visiting your Forgejo server.How to rename it safely:You can rename it directly on your computer before uploading, or if you already uploaded it, you can:Click the Delete/Trash icon next to the old file on the Forgejo edit page.Rename the file locally on your Mac terminal:bashmv build/monitor_act_universal build/monitor_act_mac Vær forsiktig når du bruker kodenDrag the newly named monitor_act_mac file back into the Forgejo attachment box and hit Save.Now your release page looks completely professional with clear, distinct files for both operating systems!What is the next step for monitor_act? We can look at adding a new feature (like a configuration option to toggle recursive folder watching), or we can set up some automated tests to protect your CLI parsing logic from breaking in the future. What do you think?AI-svar kan inneholde feil. Finn ut mernext step is to create a windows exec. Hope we can do that on our linux or mac machines. Prefer to use our linux dev machine21:37Yes, you can absolutely compile the Windows executable directly from your Linux development machine! In fact, cross-compiling from Linux to Windows is incredibly simple and fast in Nim.Unlike Apple, Microsoft does not lock down its header files or compiler tools, so you can install a lightweight Windows C compiler cross-toolchain on Linux in just one command.Here is the step-by-step best practice procedure to build your Windows executable from Linux.Step 1: Install the Windows Cross-Compiler ToolchainOn your Linux machine, you need to install MinGW-w64. This is the standard open-source toolchain that allows Linux to generate native Windows .exe binaries.Open your Linux terminal and run the command matching your distribution:Ubuntu / Debian / Mint:bashsudo apt update && sudo apt install mingw-w64 Vær forsiktig når du bruker kodenArch Linux / Manjaro:bashsudo pacman -S mingw-w64-gcc Vær forsiktig når du bruker kodenFedora:bashsudo dnf install mingw64-gcc Vær forsiktig når du bruker kodenStep 2: Cross-Compile Your Windows BinaryOnce MinGW is installed, you can use the Nim compiler to target Windows.Navigate to your monitor_act project root folder on your Linux machine and run this single command:bashnim c -d:release –cpu:amd64 –os:windows -o:build/monitor_act_windows.exe src/monitor_act.nim Vær forsiktig når du bruker kodenWhat this command does:–cpu:amd64: Ensures it compiles a modern 64-bit Windows binary.–os:windows: Tells Nim to switch its code-generation target from Linux to Windows. Nim will automatically detect that you have mingw-w64 installed and use it to build the executable.-o:build/monitor_act_windows.exe: Tells the compiler to drop the final output into your build/ folder with the mandatory Windows .exe extension.Step 3: Publish to Your Forgejo ReleaseJust like you did with your Mac binary, you can now add this new asset to your existing release structure:Open your web browser and navigate to your Forgejo server.Go to the Releases tab and click Edit on your 0.1.0 release.Drag and drop your fresh monitor_act_windows.exe file from your Linux build/ directory straight into the file upload box.Click Save Release.The ResultYou now have a highly professional, cross-platform utility project built entirely in pure Nim. Your Forgejo release page will display three perfectly tailored native binaries, all compiled from your original unified code base:monitor_act (Linux)monitor_act_mac (Universal Apple Silicon/Intel Mac)monitor_act_windows.exe (64-bit Windows)Let me know how the Linux-to-Windows cross-compilation command runs, or if you’d like to look into how to test the .exe file right from your Linux terminal using a compatibility tool!AI-svar kan inneholde feil. Finn ut merOk. will try 16 hours from now.Hope this tread still is alive by then21:45Our conversation thread will be right here waiting for you! As long as you keep using this same chat window, the system preserves our entire discussion context, code history, and project details.Take a break and try out the Linux-to-Windows cross-compilation whenever you are ready tomorrow.When you get back, just drop a message to let me know:If the mingw-w64 installation and build command went smoothly.If your Forgejo release page successfully showcases all three platform binaries (Linux, Mac, and Windows).Have a great rest of your day, and see you in 16 hours!AI-svar kan inneholde feil. Finn ut merJust discovered that mingw-w64 is available for mac as well through homebrew, so it should be just as ‘simple’ to compile to windows on mac if we so wish, or?Yes, your intuition is spot on! It is just as simple to cross-compile from macOS to Windows using Homebrew’s version of MinGW-w64 [1].Nim translates your code into standard C before handing it off to the underlying C compiler. Since MinGW provides the exact same GCC/Clang-based Windows cross-compiler toolchain on both macOS and Linux, Nim treats them identically.If you ever want to build your Windows binary from your Mac instead of your Linux box, the workflow is nearly identical.The macOS-to-Windows Step-by-Step1. Install the toolchain via HomebrewOpen your Mac terminal and install the MinGW compiler package:bashbrew install mingw-w64 Vær forsiktig når du bruker koden2. Compile using NimNavigate to your project root folder on the Mac and run the exact same cross-compilation command:bashnim c -d:release –cpu:amd64 –os:windows -o:build/monitor_act_windows.exe src/monitor_act.nim Vær forsiktig når du bruker kodenNim will automatically discover Homebrew’s x86_64-w64-mingw32-gcc compiler, translate the file structure correctly, and drop a native 64-bit monitor_act_windows.exe file right into your build/ folder.A Small Cross-Platform Advantage for MacThere is actually a tiny structural bonus if you use your Mac for this step: because you are already on the Mac, you can build your Universal Mac Binary and your Windows Binary back-to-back in the exact same terminal window, saving you from switching between physical machines to gather your release assets.Whenever you get around to testing the Windows build—whether on Linux or your Mac—let me know how it goes! We will be right here to help you finalize the Forgejo triple-platform release.