Cosmopolitan Libc makes C/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 7.3 + NetBSD + BIOS with the best possible performance and the tiniest footprint imaginable.
For an introduction to this project, please read the actually portable executable blog post and cosmopolitan libc website. We also have API documentation.
You can start by obtaining a release of our cosmocc compiler from https://cosmo.zip/pub/cosmocc/.
Here's an example program we can write:
It can be compiled as follows:
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:
Here's how you can get a much more verbose log of function calls:
You can use the Cosmopolitan's toolchain to build conventional open source projects which use autotools. This strategy normally works:
Cosmopolitan can be compiled from source on any of our supported platforms. The Makefile will download cosmocc automatically.
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 bootstrap your build, you can install Cosmopolitan Make from this site:
https://cosmo.zip/pub/cosmos/bin/make
E.g.:
After you've built the repo once, you can also use the make from your cosmocc at .cosmocc/current/bin/make. You might even prefer to alias make to $COSMO/.cosmocc/current/bin/make.
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.
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:
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:
You can furthermore cut out the bloat of other operating systems, and have Cosmopolitan become much more similar to Musl Libc.
For further details, see //build/config.mk.
To print a log of system calls to stderr:
To print a log of function calls to stderr:
Both strace and ftrace use the unbreakable kprintf() facility, which is able to be sent to a file by setting an environment variable.
Here's the recommended ~/.gdbinit config:
You normally run the .dbg file under gdb. If you need to debug the `` file itself, then you can load the debug symbols independently as
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:
You can fix that by registering APE with binfmt_misc:
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:
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:
The Cosmopolitan development team collaborates on the Redbean Discord server. You're welcome to join us! https://discord.gg/FwAVVu7eJ4
| AMD | K8 | 2003 |
| Intel | Core | 2006 |
| Linux | 2.6.18 | 2007 |
| Windows | 8 [1] | 2012 |
| Darwin (macOS) | 23.1.0+ | 2023 |
| OpenBSD | 7.3 or earlier | 2023 |
| 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.
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.
.png)



