Cosmopolitan makes C a build-once run-anywhere language, similar to Java, except it doesn't require interpreters or virtual machines be installed beforehand. Cosmo provides the same portability benefits as high-level languages like Go and Rust, but it doesn't invent a new language and you won't need to configure a CI system to build separate binaries for each operating system. What Cosmopolitan focuses on is fixing C by decoupling it from platforms, so it can be pleasant to use for writing small unix programs that are easily distributed to a much broader audience.
Assuming you have GCC on Linux, then all you need are the five additional files which are linked below:
# create simple c program on command line printf %s ' main() { printf("hello world\n"); } ' >hello.c # run gcc compiler in freestanding mode gcc -g -Os -static -fno-pie -no-pie -nostdlib -nostdinc \ -fno-omit-frame-pointer -pg -mnop-mcount -mno-tls-direct-seg-refs \ -o hello.com.dbg hello.c -Wl,--gc-sections -fuse-ld=bfd \ -Wl,-T,ape.lds -include cosmopolitan.h crt.o ape.o cosmopolitan.a objcopy -S -O binary hello.com.dbg hello.com # NOTE: scp it to windows/mac/etc. *before* you run it! # ~40kb static binary (can be ~16kb w/ MODE=tiny) ./hello.com
The above command fixes GCC so it outputs portable binaries that will
run on every Linux distro in addition to Mac OS X, Windows NT,
FreeBSD, OpenBSD, and NetBSD too. For details on how this works,
please read the αcτµαlly
pδrταblε εxεcµταblε blog post. This novel binary format is also
optional, since hello.com.dbg
is executable too, only on
your local system since it's an ELF binary.
Your program will also boot on bare metal too. In other words, you've written a normal textbook C program, and thanks to Cosmopolitan's low-level linker magic, you've effectively created your own operating system which happens to run on all the existing ones as well. Now that's something no one's done before.