cosmopolitan libc

your build-anywhere run-anywhere c library

Cosmopolitan Libc makes C a build-anywhere run-anywhere language, like Java, except it doesn't need an interpreter or virtual machine. Instead, it reconfigures stock GCC and Clang to output a POSIX-approved polyglot format that runs natively on Linux + Mac + Windows + FreeBSD + OpenBSD + NetBSD + BIOS on AMD64 and ARM64 with the best possible performance.

Getting Started

First, download the Cosmopolitan toolchain:

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

Sanity Test

Next, let's make sure you can run APE programs on your system:

bin/make --version

If you get "zsh: exec format error" then you need to upgrade to zsh 5.9+ (we patched it 2 years ago). The same goes for Fish.

Linux users will want to install APE Loader. This will solve issues like WINE trying to run APE, or "run-detectors: unable to find an interpreter".

# for linux users
sudo wget -O /usr/bin/ape https://cosmo.zip/pub/cosmos/bin/ape-$(uname -m).elf
sudo chmod +x /usr/bin/ape
sudo sh -c "echo ':APE:M::MZqFpD::/usr/bin/ape:' >/proc/sys/fs/binfmt_misc/register"
sudo sh -c "echo ':APE-jart:M::jartsr::/usr/bin/ape:' >/proc/sys/fs/binfmt_misc/register"

If you're on Windows, try renaming bin/make to bin/make.exe and run that. Then rename it back. Since cosmocc is a shell script, you need a UNIX shell. You can get programs like bash, less, etc. by downloading Cosmos programs and putting them in C:\bin. ProTip: dash makes a pretty good C:\bin\sh (which in cosmo speak is /bin/sh or /c/bin/sh). Next, install Terminal Preview from the Windows Store and configure it so that C:\bin\bash -l is your shell. That way, the lack of a .exe extension will no longer be an issue.

WSL users will want to run this command to prevent Windows from running APE as WIN32 programs inside the WSL environment.

sudo sh -c "echo -1 >/proc/sys/fs/binfmt_misc/WSLInterop"

Compiling Hello World

Create a file named hello.c:

#include <stdio.h>

int main(int argc, char *argv[]) {
  printf("hello world\n");

Then compile it as follows:

bin/cosmocc -o hello hello.c

Congratulations! You just made your first fat actually portable executable. It'll run on Linux/MacOS/Windows/FreeBSD/NetBSD/OpenBSD on the AMD64 and ARM64 architectures.

Further Reading

For further getting started documentation, please see:

For high-quality example code on how to develop C programs with Cosmopolitan Libc, see:

For high-quality example code on how to build open source projects with Cosmopolitan Libc, see:

Prebuilt Software

For prebuilt fat binaries of popular open source software, please visit our online service: