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

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 <stdio.h>
int main() { 
    printf("hello world\\n"); 
}

Compile and run it:

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

Boom!



0
0
0.000
2 comments
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

Consider setting @stemsocial as a beneficiary of this post's rewards if you would like to support the community and contribute to its mission of promoting science and education on Hive. 
 

0
0
0.000
avatar

No one gives a shit 💩 over rewarded trash 🗑️🚮 looks who voting on this post you people have no sense 😂😂😂😂😂😂

Truth hurts doesn't it 😂😂😂

💯 Original Content and comments downvoted for no reason Please read and view

@steevc please tell your friends to stop downvoting my original content please 🙏🏾

Blocktrades Aka @themarkymark Aka @buildawhale @punkteam @usainvote @gogreenbuddy @letsusbuyhive please stop downvoting my original content I'm not going to reply to your abusive comments as that is what you enjoy doing seems like you have a lot of free time

Please explain why you keep downvoting my original content 🤔 if you aren't blocktrades then why doesn't do something 😂 also why would you be sending all that Hive with buildawhale to alpha an account that is blocktrades wife 😂😂😂

@crimsonclad please do your job 🙏🏾

https://hive.blog/hive-135178/@crimsonclad/re-kgakakillerg-sxllhv

https://hive.blog/hive-148441/@hivewatchers/svftu9

https://hive.blog/hive-148441/@hivewatchers/svdjjz

https://hive.blog/hive-176853/@steevc/re-kgakakillerg-syyy4x

https://hive.blog/dev/@howo/re-kgakakillerg-szhax7

https://hive.blog/hive/@steevc/follow-friday-respect

https://hive.blog/hive-127022/@shmoogleosukami/re-kgakakillerg-t0hcxc

It's unbelievable that they downvoted this Goodbye Auntie R.I.P 🙏🏾

Comments being downvoted by blocktrades https://hive.blog/hive-170744/@kgakakillerg/t0ns3b

https://hive.blog/hive-127466/@steevc/re-blocktrades-t0kint

https://hive.blog/hive-127466/@blocktrades/t0lq41

https://hive.blog/hive/@ureka.stats/the-untrending-report-hive-downvote-analysis-2025-06-29-20250629143829

https://hive.blog/hive-127466/@kgakakillerg/t0m1vn

https://hive.blog/hive-108278/@kgakakillerg/t0rfo8

https://hive.blog/hive-127466/@kgakakillerg/t0vcl7

Stop farming Hive https://hive.blog/burnpost/@buildawhale/re-1756307402814502169-20250827t151240z

😂😂😂😂😂

📊 The Untrending Report - Hive Downvote Analysis - 2025-09-07

RE: Understanding Pointers in C Programming

Please Peakd, allow me to completely block spammers (Would you ever vote for this?) thanks for exposing yourself more @makerhacks

Please go through it all instead of calling it spam 😂😂😂😂🤣🤣🤣🤣🤣🤣🤣

0
0
0.000