Write Once, Run Anywhere ... With C??

@makerhacks · 2025-09-13 18:31 · programming

image.png

One of the great things about the C programming language is it is highly portable. The problem is, the compiled programs you create with the language are not.

Languages like Java and frameworks like .NET get around this limitation but compiling to intermediate languages. Python avoids it entirely by having native interpreters.

Wouldn't it be cool, though, if we could compile our code once and have it run at native speeds anywhere?

Enter Cosmopolitan, and in particular, the version 3:

https://justine.lol/cosmo3/

After nearly one year of development, I'm pleased to announce our version 3.0 release of the Cosmopolitan library. The project is an entirely new animal. For starters, Mozilla sponsored our work as part of their MIECO program. Google also awarded me an open source peer bonus for my work on Cosmopolitan, which is a rare honor, and it's nice to see our project listed up there among the greats, e.g. curl, linux, etc. In terms of this release, we're living up to the great expectations you've all held for this project in a number of ways. The first is we invented a new linker that lets you build fat binaries which can run on these platforms:

AMD64 Linux MacOS Windows FreeBSD OpenBSD 7.3 NetBSD ARM64 Linux MacOS FreeBSD Windows (non-native)

I mean, this just blows me away. The brilliance (though this is just the tip of the genius that Justine has demonstrated).

One Limitation

There is one fly in the champagne that might have you going back to Java, Python, or JS, and that is the lack of GUI support. Your code is cross-platform provided you stick to the terminal.

It's a shame, but understandable. We do have QT, SDL, GTK, C++ Builder, and such if developing in C for multiple platforms is important to us.

That said, I can see using compiled C wrappers and native GUI tools still being very useful.

Version 3 Improvements

At version 3 they even use their own compiled compiler to compile their tools:

One of the things we're most happy with, is that Cosmo's cross platform support is now good enough to support Cosmo development.

Shockingly, in many cases using their output is more performant than the native builds from OS project maintainers!

However portability isn't the only selling point. Cosmo Libc will make your software faster and use less memory too.

You can see more about that in the pages linked above.

Example Projects

So what has been built using this? Quite a lot, but these are the ones that most impressed me.

image.png

Cross-platform NES emulator that runs in your terminal: https://justine.lol/nesemu1.html

Joel Yliluoma wrote this NES emulator back in 2011. It's one of the most beautiful pieces of code I've seen. However it used SDL as the GUI layer. I ported it to run within terminals.

Single file, high performance web server: https://redbean.dev/

redbean is an open source webserver in a single-file that runs natively on six OSes for both AMD64 and ARM64. Basic idea is if you want to build a web app that runs anywhere, then you download the redbean.com file, put your .html and .lua files inside it using the zip command, and you've got a hermetic app you deploy and share. redbean embeds Lua, SQLite, and MbedTLS into a fork() driven application server that benchmarks at 5.3 million qps on Threadripper.

If you want the above programs right away, just download the .zip that contains these plus a whole bunch of CLI tools that Linux fans will be right at home with but will run on Windows and MacOS natively too!

How to get started:

Install the compiler:

mkdir cosmocc
cd cosmocc
wget https://cosmo.zip/pub/cosmocc/cosmocc-4.0.2.zip
unzip cosmocc-4.0.2.zip

Create a test program:

#include 
int main() { 
    printf("hello world\\n"); 
}

Compile and run it:

bin/cosmocc -o hello hello.c
./hello

Boom!

#programming #technology #hivedev #gamedev
Payout: 0.000 HBD
Votes: 278
More interactions (upvote, reblog, reply) coming soon.