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 to output a POSIX-approved polyglot format that runs natively on Linux + Mac + Windows + FreeBSD + OpenBSD + NetBSD + BIOS or UEFI with the best possible performance and the tiniest footprint imaginable.
Getting Started
If you use Linux, then you can build Cosmopolitan and its included software from source as follows:
wget https://justine.lol/cosmopolitan/cosmopolitan-2.0.tar.gz
tar xf cosmopolitan-2.0.tar.gz
cd cosmopolitan
build/bootstrap/make.com -j8
o//examples/hello.com
If you're doing your development work on Linux or BSD and you want to bring your own build system, then you need just five files to get started:
wget https://justine.lol/cosmopolitan/cosmopolitan-amalgamation-2.0.zip
unzip cosmopolitan-amalgamation-2.0.zip
printf 'main() { printf("hello world\\n"); }\n' >hello.c
gcc -g -Os -static -nostdlib -nostdinc -fno-pie -no-pie -mno-red-zone \
-fno-omit-frame-pointer -pg -mnop-mcount -mno-tls-direct-seg-refs \
-o hello.com.dbg hello.c -fuse-ld=bfd -Wl,-T,ape.lds -Wl,--gc-sections \
-include cosmopolitan.h crt.o ape-no-modify-self.o cosmopolitan.a
objcopy -S -O binary hello.com.dbg hello.com
Breaking Changes
- You have to use
/zip/...
rather thanzip:...
when open()'ing assets stored in the PKZIP structure of your executable program. 00611e9 - You should use
/c/...
paths on Windows rather thanC:/...
. Your arguments and environment variables will be patched conservatively by the runtime to normalize$PATH
and make life easy. It's still possible to use DOS, WIN32, NT, and other Microsoft-style paths with our APIs. - The default stack size has been reduced to 128kb. To learn how to increase this limit, or log your stack usage, please see 226aaf3
- We renamed
LOGF
toINFOF
in the logging API. 0584684 - We no longer define
intmax_t
as 128-bit. We now violate the standard like everyone else, by defining it as 64-bit. We've introduced a new%jjd
formatting directive forint128_t
. Functions likebsrmax()
are now calledbsr128()
. 868af3f - We've renamed
nothrow
todontthrow
andnodiscard
is nowdontdiscard
. 868af3f - Remove undocumented and ANSI WIN32 APIs in d3b599a and a157940
Features
- We now favor the new APE loader (see ape-no-modify-self.o). It's extracted automatically. It's fast. It maps your programs into memory without copying or self-modifying the header. An
--assimilate
flag is baked-in to the shell script at the tops of your binaries, so you can convert them to the platform-local format whenever you want. If you're using binfmt_misc then a separate assimilate program is provided for doing just that. We also still distribute the classic ape.o bootloader. 47a53e1 - Binfmt_misc support for Linux users. This is the fastest way to run APE programs. You can install it by running the
ape/apeinstall.sh
program in this repository. - We've polyfilled the OpenBSD pledge() and unveil() system calls for Linux. You can now use these APIs in your C/C++/Python/Redbean programs, or you can use the new pledge.com command to sandbox your existing Linux userspace.
- We now use Landlock Make to build the repository, since it offers sandboxing and hermeticity guarantees. Our project will still build fine using vanilla GNU Make, since Landlock Make is backwards compatible.
- We now offer support for threading that works on all six supported operating systems. Use the
_spawn()
and_join()
functions. We're working on POSIX Threads support and recommend using our newpthread_mutex_lock
implementation. For examples of multi-threaded programs, see examples/greenbean.c and tool/build/mkdeps.c. - You can now use the
_Thread_local
keyword. Your C runtime will now always initialize Thread-Local Storage (TLS) for your main process thread. Doing that means the minimum APE binary size had to be increased from 12kb to 16kb. We prefer using the%fs
register. You must use the-mno-tls-direct-seg-refs
flag. On Windows and Apple platforms, your executable will rewritten in memory at initialization to turn%fs
opcodes into%gs
. If you wish to control your segment registers, then you may do so by setting__tls_enabled
to false and callingarch_prctl
. Please note if you use threads, this will makeerrno
no longer thread safe. - We now uniquely polyfill
mmap(MAP_STACK)
which brings the FreeBSD behavior to Linux. You must use it if you want your code to run on OpenBSD. Please see the mmap() documentation to learn more. - The Cosmopolitan monolithic repository now offers the LLVM LIBCXX Standard Template Library for C++ developers. This is not currently available in the amalgamation.
- Your Cosmopolitan binaries now include an
--ftrace
flag which logs C function calls to standard error. Please read the Logging C Functions blog post which explains more. You need to use the-pg
(and hopefully also-mnop-mcount
) flag for this to work. If you don't want this functionality in your runtime, useMODE=tiny
orMODE=rel
. - Your Cosmopolitan binaries now include an
--strace
flag which logs system calls to standard error. This works consistently across platforms. It's especially helpful for ZIP-related system calls, which aren't handled by the kernel. Please read the Logging C Functions blog post which explains more. If you don't want this functionality in your runtime, useMODE=tiny
orMODE=rel
. 14e192e - You can now use Actually Portable Python by building
o//third_party/python/python.com
which includes animport cosmo
module that lets you use our best features too. - New
closefrom()
system call for Linux 5.9+, FreeBSD 8+, and OpenBSD. - The
clock_gettime
andgettimeofday
functions now go 10x faster on Linux via the vDSO. - Our ASAN module is now able to trace the origin of memory allocations when generating reports.
- Complex math functions are now available, thanks to Musl Libc.
- Many math functions, e.g. pow(), now go faster, thanks to ARM.
- New
nointernet()
function which is similar in spirit to djb'sdisablenetwork()
function. - New string appending library. See appendd, appendf, appendr, appends, appendw, appendz, kappendf, kvappendf, and vappendf. What makes this library good is it's (a) fast, and (b) strings made by these functions can be passed to free().
- We now offer a bulletproof unbreakable kprintf() family of functions as part of the
privileged
runtime. - SSL performance is now significantly improved. See 398f0c1 and f3e28aa and similar changes where we've added x86 optimizations for computing SHA faster, Intel ADX code that makes RSA multiplication faster, clean up code to make Everest curve25519 faster, and wrote an assembly implementation of NIST curve quasi-reduction.
Bug Fixes
- Fork on Windows now works reliably 0cb6b6f 98909b1 e5314de
- Memory maps on Windows now work reliably
- Signals on Windows now works well enough for Redbean to offer a web server with a repl 072e1d2
- Fix %c with nul character 3c28533
- There's been a lot of fixes and fine tuning to the system call support magnums in consts.sh and syscalls.sh
- Many system call polyfills have been improved, e.g. sysinfo, statfs, pselect, ppoll, sleep, sched_yield, utimensat, setsockopt, clock_gettime, sigsuspend, etc.
- We now polyfill ENOENT vs. ENOTDIR on Windows
- Daemonization now works on Windows