Cosmopolitan Libc 使C语言实现像 Java 一样一次编写到处运行的特性,而且不需要 JVM 之类的虚拟机,代码是纯原生的。它可以在 Linux + Mac + Windows + FreeBSD + OpenBSD + NetBSD + BIOS 上执行,保持了C语言的高性能与小体积。
有关其可移植的原理,改日另写一篇博客加以分析。
HelloWorld
非常简单,甚至不需要特别加以说明
wget https://justine.lol/cosmopolitan/cosmopolitan-amalgamation-1.0.zip
unzip cosmopolitan-amalgamation-1.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 \
-o hello.com.dbg hello.c -fuse-ld=bfd -Wl,-T,ape.lds \
-include cosmopolitan.h crt.o ape.o cosmopolitan.a
objcopy -S -O binary hello.com.dbg hello.com
./hello.com
生成的文件在首次执行时会改变自身到该平台下的可执行文件格式。
base16384
详见 base16384,使用actions+macro实现自动构建全平台通用的base16384.com
。该文件也可在这里下载。
name: release
on:
push:
tags:
- v*
env:
GITHUB_TOKEN: ${{ github.token }}
jobs:
my-job:
name: Build on Push Tag 🚀
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v2
- name: Set up Build tools
run: |
sudo apt-get update
sudo apt-get install -y gcc
wget https://justine.lol/cosmopolitan/cosmopolitan-amalgamation-1.0.zip
unzip cosmopolitan-amalgamation-1.0.zip
- name: Build Cosmopolitan
run: |
gcc -g -Os -static -nostdlib -nostdinc -fno-pie -no-pie -mno-red-zone \
-fno-omit-frame-pointer -pg -mnop-mcount -D__cosmopolitan \
-o base16384.com.dbg base16384.c base1432.c base14.c -fuse-ld=bfd -Wl,-T,ape.lds \
-include cosmopolitan.h crt.o ape.o cosmopolitan.a
objcopy -S -O binary base16384.com.dbg base16384.com
- name: Upload binaries to release
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: base16384.com
tag: ${{ github.ref }}
overwrite: true
file_glob: true