build-once run-anywhere c library
Go to file
Justine Tunney 1bd69867d1
Explain how to build FOSS in cosmocc README
2023-12-09 06:58:24 -08:00
.github Reconfigure GitHub Actions 2023-07-10 12:17:18 -07:00
.vscode Remove IMAGE_BASE_VIRTUAL 2023-09-12 01:21:36 -07:00
ape Add <link.h> for absl 2023-12-08 20:04:10 -08:00
build Exclude strace from MODE=tiny builds 2023-11-29 03:45:54 -08:00
dsp flip et / noet in modelines 2023-12-07 22:17:11 -05:00
examples flip et / noet in modelines 2023-12-07 22:17:11 -05:00
libc Move `tcgetpgrp` and `tcsetpgrp` decls to `unistd.h` (#996) (#997) 2023-12-08 22:54:46 -08:00
net flip et / noet in modelines 2023-12-07 22:17:11 -05:00
test flip et / noet in modelines 2023-12-07 22:17:11 -05:00
third_party Add libresolv from Musl Libc 2023-12-08 20:04:10 -08:00
tool Explain how to build FOSS in cosmocc README 2023-12-09 06:58:24 -08:00
usr/share Losslessly recompress PNG files (#877) 2023-07-31 17:59:16 -07:00
.clang-format clang-format: merge include blocks (#792) 2023-03-29 00:05:58 -07:00
.gitattributes Make improvements 2023-10-15 16:45:00 -07:00
.gitignore Introduce prompt caching so prompts load instantly 2023-04-28 16:15:26 -07:00 Add 2021-04-11 00:55:53 -07:00
LICENSE Add title to the LICENSE file 2021-02-27 13:25:59 -08:00
Makefile Add libresolv from Musl Libc 2023-12-08 20:04:10 -08:00 Add appropriate chmod to README instructions 2023-11-30 03:35:17 -08:00

Cosmopolitan Honeybadger



Cosmopolitan Libc makes C a build-once 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 with the best possible performance and the tiniest footprint imaginable.


For an introduction to this project, please read the αcτµαlly pδrταblε εxεcµταblε blog post and cosmopolitan libc website. We also have API documentation.

Getting Started

You can start by obtaining a release of our cosmocc compiler from

mkdir -p cosmocc
cd cosmocc

Here's an example program we can write:

// hello.c
#include <stdio.h>

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

It can be compiled as follows:

cosmocc -o hello hello.c

The Cosmopolitan Libc runtime links some heavyweight troubleshooting features by default, which are very useful for developers and admins. Here's how you can log system calls:

./hello --strace

Here's how you can get a much more verbose log of function calls:

./hello --ftrace

You can use the Cosmopolitan's toolchain to build conventional open source projects which use autotools. This strategy normally works:

export CC=x86_64-unknown-cosmo-cc
export CXX=x86_64-unknown-cosmo-c++
./configure --prefix=/opt/cosmos/x86_64
make -j
make install

Cosmopolitan Source Builds

Cosmopolitan can be compiled from source on any of our supported platforms. First, you need to download or clone the repository. If you're not using x86-64 Linux then you'll need cosmocc too.

git clone cosmo
cd cosmo
mkdir -p o/third_party/gcc
pushd o/third_party/gcc

It's recommended that you install a systemwide APE Loader. This command requires sudo access to copy the ape command to a system folder and register with binfmt_misc on Linux, for even more performance.


You can now build the mono repo with any modern version of GNU Make. To make life easier, we've included one in the cosmocc toolchain, which is guaranteed to be compatible and furthermore includes our extensions for doing build system sandboxing.

o//third_party/gcc/bin/make -j8

Since the Cosmopolitan repository is very large, you might only want to build one particular thing. Here's an example of a target that can be compiled relatively quickly, which is a simple POSIX test that only depends on core LIBC packages.

rm -rf o//libc o//test
o//third_party/gcc/bin/make o//test/posix/

Sometimes it's desirable to build a subset of targets, without having to list out each individual one. For example if you wanted to build and run all the unit tests in the TEST_POSIX package, you could say:

o//third_party/gcc/bin/make o//test/posix

Cosmopolitan provides a variety of build modes. For example, if you want really tiny binaries (as small as 12kb in size) then you'd say:

o//third_party/gcc/bin/make m=tiny

You can furthermore cut out the bloat of other operating systems, and have Cosmopolitan become much more similar to Musl Libc.

o//third_party/gcc/bin/make m=tinylinux

For further details, see //build/


To print a log of system calls to stderr:

cosmocc -o hello hello.c
./hello --strace

To print a log of function calls to stderr:

cosmocc -o hello hello.c
./hello --ftrace

Both strace and ftrace use the unbreakable kprintf() facility, which is able to be sent to a file by setting an environment variable.

export KPRINTF_LOG=log
./hello --strace


Here's the recommended ~/.gdbinit config:

set host-charset UTF-8
set target-charset UTF-8
set target-wide-charset UTF-8
set osabi none
set complaints 0
set confirm off
set history save on
set history filename ~/.gdb_history
define asm
  layout asm
  layout reg
define src
  layout src
  layout reg

You normally run the .com.dbg file under gdb. If you need to debug the .com file itself, then you can load the debug symbols independently as

gdb -ex 'add-symbol-file 0x401000'

Platform Notes


If you use zsh and have trouble running APE programs try sh -c ./prog or simply upgrade to zsh 5.9+ (since we patched it two years ago). The same is the case for Python subprocess, old versions of fish, etc.


Some Linux systems are configured to launch MZ executables under WINE. Other distros configure their stock installs so that APE programs will print "run-detectors: unable to find an interpreter". For example:

jart@ubuntu:~$ wget
jart@ubuntu:~$ chmod +x dash
jart@ubuntu:~$ ./dash
run-detectors: unable to find an interpreter for ./dash

You can fix that by registering APE with binfmt_misc:

sudo wget -O /usr/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"

You should be good now. APE will not only work, it'll launch executables 400µs faster now too. However if things still didn't work out, it's also possible to disable binfmt_misc as follows:

sudo sh -c 'echo -1 > /proc/sys/fs/binfmt_misc/cli'     # remove Ubuntu's MZ interpreter
sudo sh -c 'echo -1 > /proc/sys/fs/binfmt_misc/status'  # remove ALL binfmt_misc entries


It's normally unsafe to use APE in a WSL environment, because it tries to run MZ executables as WIN32 binaries within the WSL environment. In order to make it safe to use Cosmopolitan software on WSL, run this:

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

Discord Chatroom

The Cosmopolitan development team collaborates on the Redbean Discord server. You're welcome to join us!

Support Vector

Platform Min Version Circa
AMD K8 Venus 2005
Intel Core 2006
Linux 2.6.18 2007
Windows 8 [1] 2012
Mac OS X 15.6 2018
OpenBSD 7 2021
FreeBSD 13 2020
NetBSD 9.2 2021

[1] See our vista branch for a community supported version of Cosmopolitan that works on Windows Vista and Windows 7.

Special Thanks

Funding for this project is crowdsourced using GitHub Sponsors and Patreon. Your support is what makes this project possible. Thank you! We'd also like to give special thanks to the following groups and individuals:

For publicly sponsoring our work at the highest tier.