HackerReposA FIELD GUIDE TO OPEN SOURCE
ProSponsor the shelf♧ 0
← Back to top all-time
jart
cosmopolitan
C2020–2026
jart / cosmopolitan

cosmopolitan

build-once run-anywhere c library

135Distinct people
293Mentions
3Explicit recommenders
21.4KGitHub stars
REPOSITORY AT A GLANCE

What to know before exploring

What it is

build-once run-anywhere c library

GitHub

Where it is used

  • Blink — Blink uses Cosmopolitan Libc as the primary target for the binaries it runs. It is designed to support Cosmopolitan-built APE executables and relies on them heavily in testing. Source

What people are saying

JVM bytecode is obviously not what I was talking about - it is neither low-level (if you can't efficiently compile the entirety of C into it, it's not good enough for this…

int_19h used a recommendation phrase. 8 indexed comments match. This is a phrase match, not a verdict.

No classified warning is attached to this page. Read the discussions before treating silence as approval.

Also in these discussions

Mentioned in the same Hacker News threads. That is not proof they are substitutes.

This week on Hacker News

Nov 9–Nov 16 UTC · Direct repository links and separately labeled related project stories. Indexed through 11/16/2026, 8:59:02 AM UTC.

No direct repository links were found in the indexed HN discussions over the past seven days. Older discussions are available below.

Mentions over time

HN mentions by year293 dated mentions

Observed mentions: 2020-06-20 21:16:58 UTC to 2026-10-11 05:47:52 UTC.

02040602020-01-01 00:00:00 UTC to 2021-01-01 00:00:00 UTC (exclusive): 26 mentions; 1 submissions, 25 comments20202021-01-01 00:00:00 UTC to 2022-01-01 00:00:00 UTC (exclusive): 69 mentions; 4 submissions, 65 comments2022-01-01 00:00:00 UTC to 2023-01-01 00:00:00 UTC (exclusive): 69 mentions; 5 submissions, 64 comments2023-01-01 00:00:00 UTC to 2024-01-01 00:00:00 UTC (exclusive): 35 mentions; 1 submissions, 34 comments20232024-01-01 00:00:00 UTC to 2025-01-01 00:00:00 UTC (exclusive): 55 mentions; 9 submissions, 46 comments2025-01-01 00:00:00 UTC to 2026-01-01 00:00:00 UTC (exclusive): 22 mentions; 3 submissions, 19 comments2026-01-01 00:00:00 UTC to 2027-01-01 00:00:00 UTC (exclusive): 17 mentions; 1 submissions, 16 comments. Incomplete calendar period.2026*

* An incomplete calendar period is marked with an outlined bar. Its count is not directly comparable with a full period.

Counts come from the repository’s dated source evidence. Index cutoff: 2026-11-02 08:16:59 UTC.

View mention counts and date ranges
UTC ranges include their start and exclude their end. Select a period to filter the source evidence.
PeriodStart (UTC)End (exclusive, UTC)MentionsSubmissionsComments
20202020-01-01 00:00:00 UTC2021-01-01 00:00:00 UTC26125
20212021-01-01 00:00:00 UTC2022-01-01 00:00:00 UTC69465
20222022-01-01 00:00:00 UTC2023-01-01 00:00:00 UTC69564
20232023-01-01 00:00:00 UTC2024-01-01 00:00:00 UTC35134
20242024-01-01 00:00:00 UTC2025-01-01 00:00:00 UTC55946
20252025-01-01 00:00:00 UTC2026-01-01 00:00:00 UTC22319
2026 (partial)2026-01-01 00:00:00 UTC2027-01-01 00:00:00 UTC17116

From the project README

An excerpt from the repository, fetched Nov 12, 2026.

Read the project’s introduction
Cosmopolitan 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. Background For an introduction to this project, please read the actually portable executable 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 https://cosmo.zip/pub/cosmocc/. mkdir -p cosmocc cd cosmocc wget https://cosmo.zip/pub/cosmocc/cosmocc.zip unzip cosmocc.zip 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 ./hello 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. 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. ape/apeinstall.sh 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.: curl -LO https://cosmo.zip/pub/cosmos/bin/make ./make -j8 o//examples/hello 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. rm -rf o//libc o//test .cosmocc/current/bin/make o//test/posix/signal_test o//test/posix/signal_test 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: .cosmocc/current/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: .cosmocc/current/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. .cosmocc/current/bin/make m=tinylinux For further details, see //build/config.mk. Debugging 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 GDB 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 end define src layout src layout reg end src 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 gdb foo -ex 'add-symbol-file foo.dbg 0x401000' Platform Notes Shells 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. Linux 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 https://cosmo.zip/pub/cosmos/bin/dash 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 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" 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 WSL 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! https://discord.gg/FwAVVu7eJ4 Support Vector Platform Min Version Circa AMD K8 2003 Intel Core 2006 Linux 2.6.18 2007 Windows 8 [1] 2012 Darwin (macOS) 2
Read the full README on GitHub

What Hacker News says

26 sources
The compiler keeps the branch. The branch doesn't impact performance because it's fully predictable and therefore costs less than 1 nanosecond. Please note however that fadvise() is a system call and the SYSCALL instruction o/s context-switch generally costs 1 microsecond, which is 1000x slower than any overhead incurred by the portability benefits Cosmopolitan offers. You can however tune the build so the `IsWindows()` branch is dead code eliminated anyway: make MODE=tiny CPPFLAGS=-DSUPPORT_VECTOR=0b11111011 Basically unsetting the Windows bit causes the Windows polyfills to not be linked in your binary. It's only useful for making binaries tinier. So rest assured, if you find your 16kb APE .com binary to be too bloated, you can always use SUPPORT_VECTOR to squeeze it down to the order of a 4kb ELF executable. See: https://github.co
Read the rest
In Cosmopolitan Libc: build-once run-anywhere C library
If anyone's curious, here's the link to memcpy() as it's actually implemented in the Cosmopolitan headers: https://github.com/jart/cosmopolitan/blob/de09bec215675e9b0b... One thing that the web page doesn't mention (for the sake of simplicity) is that the Cosmopolitan headers do call __builtin_memcpy() as well, but only for 2-power constexpr sizes. That's the only time when GCC and Clang both do the optimal thing. In all other cases it's faster to use asm("call MemCpy") which implements something faster than the builtin would otherwise generate. See https://github.com/jart/cosmopolitan/blob/de09bec215675e9b0b...
In Cosmopolitan Libc: build-once run-anywhere C library
I thought x86 was pretty straightforward once I understood the octal encoding. Cosmopolitan provides a 3kb x86 instruction length decoder that supports all past, present, and future ISAs: https://github.com/jart/cosmopolitan/blob/master/third_party... The Cosmopolitan codebase also provides a debugger / emulator for i8086 + x86-64 that's similar to GDB TUI and Bochs. See https://justine.lol/blinkenlights/index.html For example, here's the ALU code: https://github.com/jart/cosmopolitan/blob/master/tool/build/...
In Cosmopolitan Libc: build-once run-anywhere C library
Author here. That's not an inherent limitation. See https://justine.lol/apelife/index.html for an example of a GUI + TUI that's built using Cosmopolitan. The reason why I said what you quoted, is I'm simply trying to calibrate expectations. I don't view desktop GUIs as a productive area of focus, because there's such a lack of consensus surrounding the APIs that requires, and web browsers do a great job. As for sizeof(long) it's 64-bit with Cosmopolitan, which uses an LP64 data model. That hasn't impacted its ability to use the WIN32 API. See https://github.com/jart/cosmopolitan/tree/master/libc/nt where WIN32 API functions are declared using uint32_t, int64_t, etc. It works great. I compile my Windows apps on Linux and it feels wonderful to not have to touch MSVC. Cosmopolitan even takes care of the chore of translating your UTF-8 string
Read the rest
s into UTF-16 when polyfilling functions like open() which delegates to CreateFile() on Windows.
In Cosmopolitan Libc: build-once run-anywhere C library
Author here. The example and repository has been updated. Many eyes make all bugs shallow. I was hoping to do more testing before posting on Hacker News. Now that the cat's out of the bag, I'd like to explain what's being done to make sure bugs like that never happen. Cosmopolitan implements a simplified version of the Address Sanitizer (ASAN) runtime. See https://github.com/jart/cosmopolitan/blob/master/libc/log/as... I've been systematically working towards using it to vet the entire codebase. It's helped to address more than a few memory issues so far. However more work needs to be done polishing the ASAN code so it can be bootstrapped for the purposes of having memory safety in the lowest level libraries like LIBC_STR, due to the way that the Makefile requires acyclicity between libraries. This has been less of an issue, because bugs
Read the rest
are normally very evident in functions like strcpy(). The BSD version is an exception though, since it was only used to port programs from the OpenBSD codebase which were used sparingly. But in either case, the real focus here is having continual incremental improvements over time, and I'm very committed to addressing any and all concerns. Cosmopolitan also has extensive unit tests. This is something that C libraries have traditional done somewhat sparingly, if at all, since C library authors conventionally rely on an ecosystem of ported software that very quickly breaks if everything isn't perfect. If you build the Cosmopolitan repository, you'll notice there's 194 independent test executables in the o/test folder. That's one of the great things about Actually Portable Executable. It makes binaries so tiny that we can have lots of tests, completely isolated from each other!
In Cosmopolitan Libc: build-once run-anywhere C library
> Traditionally, UNIX commands were written under the assumption they were going to be used primarily by other programs [...] it’s time to shed some of this baggage UNIX commands are absolutely human first. If they weren't then the `ls` command would output a JSON PROTOBUF ASN.1 SQL DUMP rather than a plaintext list of file names. > Use a command-line argument parsing library where you can What you're doing a good job of here, is granting a loudspeaker to all the fresh community projects and excitement surrounding CLI. That's awesome. It's not appropriate for a best practices document, which should be making much more conservative recommendations, considering how prevalent arg parsers are in the standard libraries of languages. > you won’t need man pages. Not enough people use man pages If your program is something that's installed on the
Read the rest
system (i.e. it's not distributed as a single-file executable like youtube-dl) then it should have a man page. > Display output on success Very unconventional advice. Strongly disagree. > Disable color if your program is not in a terminal or the user requested it. Now this, is something that contradicts the conventional wisdom, but I wholeheartedly agree with it. I don't like playing cat and mouse games with commands that conditionally turn colors off based on isatty(). I just want color. If I didn't want color, then it's so trivial to filter it out using sed 's/\x1b\[[;[:digit:]]*m//g'. > The NO_COLOR environment variable is set. Or more conventionally, TERM=dumb or the sed above. Never heard of NO_COLOR before. > You may also want to add a MYAPP_NO_COLOR environment variable This can be seen as environment variable pollution. Those strings need to be copied every time a process is spawned. Many of the C library routines for editing the environment are O(n) unfortunately because you can't assume it's sorted like they are on Windows. > Only use prompts or interactive elements if stdin is an interactive terminal Bad advice. Many programs like ispell / aspell / etc. are interactive in nature while being used primarily through pipes as a subprocess. The best advice I can give is to just never ever check if it's a tty because it doesn't matter. Just make your program behave the same regardless of environmental conditions. Otherwise it's a huge pain to automate, and that which can't be automated isn't worth doing as a cli program. > Responsive is more important than fast. Print something to the user in <100ms That's a low bar even for a web server. It takes 25 microseconds to launch a subprocess and wait for it to complete. That's the lower bound imposed by the practicalities of the Linux implementation of a vfork->execve->exit->wait4 roundtrip, which abstracts a heroic amount of work under the hood and decades of effort at improvements. So if you can't get your CLI program to do something within at least 100 microseconds, then you've got a serious problem with bloatware or third party telemetry and should seriously reconsider your language/library/tooling stack. > TERM, TERMINFO and TERMCAP, if you’re going to use terminal-specific escape sequences. That was good advice two decades ago. Just use the VT100 codes. Even Windows CMD.EXE supports VT100. Don't bother with the termcap thing unless you're trying to support like WYSE terminals or actual teletypewriters. Here's the dox on the subject: https://github.com/jart/cosmopolitan/blob/1fc91f3/tool/build... > If a user hits Ctrl-C during clean-up operations that might take a long time, skip them. Tell the user what will happen when they hit Ctrl-C again, in case it is a destructive action. Not worth prompting. Just kill it if you ctrl-c twice. I mean, if you're going to leave database objects in an inconsistent state, I mean then don't stop unless someone runs kill -9 or pulls the power plug. But vast majority of cases, stuff can be interrupted, trust the user, the prog should not whine. > People are going to misuse your program. Be prepared for that. They will wrap it in scripts You must really hate people like me. I've been using Unix for 23 years and I'm barely a newcomer. I like it because it lets me automate things. If a command line program treats me like a user, rather than being a tool to be used, then I won't touch it.
In Command Line Interface Guidelines
Sorry here's what a build file looks like, which should hopefully clarify: https://github.com/jart/cosmopolitan/blob/master/libc/mem/me... As you can see, it's just vanilla make. The package program is just a helper that checks to make sure DIRECTDEPS variables are correct. That's the part Google said was tricky back in 2006. Have you ever seen people talk online about how it's super difficult to have a single makefile build multiple library files? Particularly static ones? Like the pitfalls of making sure the linker arguments are the correct order, or removing a dependency from one will break all the others that needed it transitively? This is how it can be done. I found out it just requires following a highly specific convention, where it's a little toilsome to type, but so rewarding once it's done. It also means I don't need to ask us
Read the rest
ers to install a cutting edge build system to build my code, since I've just seen too much drama in that space.
In Please – A cross-language build system
Reading your words, it looks like you have made a kind of make replacement that just works. Following your link, it looks like... what ? I don't know, some tool that checks some build configuration, augments C and postprocesses executable files? (Looking at https://github.com/jart/cosmopolitan it's an interesting hack. Might be useful in some cases, for example command-line tools like git.) Have you made something that looks like a better make? If so, it would be nice to know more. Thanks.
In Please – A cross-language build system
First thing every software engineer does after leaving Google is rewrite Blaze. Just like every site reliability engineer rewrites borgmon. What I chose to do myself was make Blaze happen using a Makefile config. There's a blog post somewhere where Google talks about why they switched from GNU Make to Blaze c. 2006. if I remember correctly it basically boiled down to not having strict dependency checking. So I thought, why not simply avoid their published mistakes without changing build systems? I learned everything I could about make, discovered that variables are secretly lambdas, and that enabled me to figure out a way to have a static archive package for each folder, which also avoided the need to write a makefile code generator. Then I wrote a simple C program to check incremental elf symbols so the build graph stays sanely formed. I
Read the rest
t totally got make working like a dream in a way I simply hadn't seen before. https://github.com/jart/cosmopolitan/blob/master/tool/build/...
In Please – A cross-language build system
Yeah it's pretty old. Textmode interfaces began with the teletypewriter in the nineteenth century and UNICODE began around that time too. The programming interface for these machines wasn't standardized until the 1960's with ASCII, which was extended a decade later by ANSI X3.64-1979, and then again in the 90's by UTF-8. That's the trifecta of textmode definitions which, only as of these last few years, became basically universally supported. CGA is kind of just a uint16_t[20][80] array for personal computers. See also: https://github.com/jart/cosmopolitan/blob/ea0b5d9/tool/build... You're absolutely right that the inconsistencies with textmode used to be about as awful as the inconsistencies we see with GUIs today. I'm certain things will get better if we consider that GUIs are a relatively recent invention by comparison. So it'll probab
Read the rest
ly take another fifty years for widget authors to forge out a consensus that's as good and widely supported as the one we have now for terminals.
In Bring back the ease of 80s and 90s personal computing
You're not alone in feeling that way. There's a very short list of people who've ever taken the time to fully grok Richard Stallman's Math 55 assembly notation, and they mostly work on projects like the Linux Kernel and glibc. It's designed to do anything. I've been using it the past few days to retool the standard x86_64 compiler to generate 16-bit boot code for The LISP Challenge. https://github.com/jart/cosmopolitan/blob/b6793d42d5ed6b4f78...
In Defer Reference Implementation for C

Showing the 15 most recent of 26 sources. Enable JavaScript to search and browse every indexed mention.

MAKE YOURSELF AT HOME

A few shortcuts

/
Search the collection
?
Open this guide
Esc
Close dialogs and navigation

Your saved repositories and interests stay in this browser.