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 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/.
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 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 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.
build/bootstrap/make -j8 o//examples/hello
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 build/bootstrap/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:
build/bootstrap/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:
build/bootstrap/make m=tiny
You can furthermore cut out the bloat of other operating systems, and have Cosmopolitan become much more similar to Musl Libc.
build/bootstrap/make m=tinylinux
For further details, see //build/config.mk.
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 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'
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 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
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"
The Cosmopolitan development team collaborates on the Redbean Discord server. You're welcome to join us! https://discord.gg/FwAVVu7eJ4
Platform | Min Version | Circa |
---|---|---|
AMD | K8 | 2003 |
Intel | Core | 2006 |
Linux | 2.6.18 | 2007 |
Windows | 8 [1] | 2012 |
Darwin (macOS) | 23.1.0+ | 2023 |
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.
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.
一键生成PPT和Word,让学习生活更轻松
讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。
深度推理能力全新升级,全面对标OpenAI o1
科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。
一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型
Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。
字节跳动发布的AI编程神器IDE
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。
AI助力,做PPT更简单!
咔片是一款轻量化在线演示设计工具,借助 AI 技术,实现从内容生成到智能设计的一站式 PPT 制作服务。支持多种文档格式导入生成 PPT,提供海量模板、智能美化、素材替换等功能,适用于销售、教师、学生等各类人群,能高效制作出高品质 PPT,满足不同场景演示需求。
选题、配图、成文,一站式创作,让内容运营更高效
讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和 发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。