jart/cosmopolitan
GitHub: jart/cosmopolitan
一个使 C/C++ 程序能够一次编译生成可在所有主流操作系统上原生运行的单一可执行文件的跨平台 C 库。
Stars: 21170 | Forks: 766

[](https://github.com/jart/cosmopolitan/actions/workflows/build.yml)
# Cosmopolitan
[Cosmopolitan Libc](https://justine.lol/cosmopolitan/index.html) 使 C/C++ 成为一种
“一次构建,到处运行”的语言,就像 Java 一样,但它不需要
解释器或虚拟机。相反,它重新配置了标准的 GCC 和
Clang,以输出一种经 POSIX 批准的跨平台格式,可以在
Linux + Mac + Windows + FreeBSD + OpenBSD 7.3 + NetBSD + BIOS 上原生运行,并具有
所能想象的最高性能和最小占用空间。
## 背景
有关此项目的介绍,请阅读 [actually portable
executable](https://justine.lol/ape.html) 博客文章和 [cosmopolitan
libc](https://justine.lol/cosmopolitan/index.html) 网站。我们还有
[API
文档](https://justine.lol/cosmopolitan/documentation.html)。
## 快速入门
你可以从 获取我们的 `cosmocc` 编译器版本作为开始。
```
mkdir -p cosmocc
cd cosmocc
wget https://cosmo.zip/pub/cosmocc/cosmocc.zip
unzip cosmocc.zip
```
下面是一个我们可以编写的示例程序:
```
// hello.c
#include
int main() {
printf("hello world\n");
}
```
可以按如下方式进行编译:
```
cosmocc -o hello hello.c
./hello
```
Cosmopolitan Libc 运行时默认链接了一些重量级的故障排查
功能,这些功能对开发人员和管理员非常有用。
以下是记录系统调用的方法:
```
./hello --strace
```
以下是如何获取更详细的函数调用日志:
```
./hello --ftrace
```
你可以使用 Cosmopolitan 的工具链来构建使用 autotools 的传统开源
项目。这种策略通常有效:
```
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 源码构建
Cosmopolitan 可以在我们支持的任何平台上从源码编译。Makefile 会自动下载 cosmocc。
建议你安装系统级的 APE Loader。此命令需要 `sudo` 权限,将 `ape` 命令复制到系统文件夹并在 Linux 上注册 binfmt_misc,以进一步提升性能。
```
ape/apeinstall.sh
```
现在你可以使用任何现代版本的 GNU Make 构建这个单体仓库。为了
引导你的构建,你可以从以下站点安装 Cosmopolitan Make:
https://cosmo.zip/pub/cosmos/bin/make
例如:
```
curl -LO https://cosmo.zip/pub/cosmos/bin/make
./make -j8
o//examples/hello
```
在构建过一次仓库之后,你也可以使用 cosmocc 中位于
`.cosmocc/current/bin/make` 的 make。你甚至可能会倾向于将 make 设置为 `$COSMO/.cosmocc/current/bin/make` 的别名。
由于 Cosmopolitan 仓库非常大,你可能只想构建某一个特定的内容。
下面是一个可以相对较快地编译的目标示例,它是一个简单的 POSIX 测试,仅依赖于核心的 LIBC 包。
```
rm -rf o//libc o//test
.cosmocc/current/bin/make o//test/posix/signal_test
o//test/posix/signal_test
```
有时我们希望构建一部分目标,而不必逐一列出每个目标。
例如,如果你想构建并运行 `TEST_POSIX` 包中的所有单元测试,你可以这样输入:
```
.cosmocc/current/bin/make o//test/posix
```
Cosmopolitan 提供了多种构建模式。例如,如果你想要
非常小的二进制文件(大小可小至 12kb),你可以输入:
```
.cosmocc/current/bin/make m=tiny
```
此外,你还可以剔除其他操作系统的冗余代码,使
Cosmopolitan 变得更加类似于 Musl Libc。
```
.cosmocc/current/bin/make m=tinylinux
```
有关更多详细信息,请参阅 [//build/config.mk](build/config.mk)。
## 调试
要将系统调用日志打印到 stderr:
```
cosmocc -o hello hello.c
./hello --strace
```
要将函数调用日志打印到 stderr:
```
cosmocc -o hello hello.c
./hello --ftrace
```
strace 和 ftrace 都使用了坚如磐石的 kprintf() 设施,可以通过设置环境变量将其输出到文件中。
```
export KPRINTF_LOG=log
./hello --strace
```
## GDB
以下是推荐的 `~/.gdbinit` 配置:
```
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
```
你通常在 gdb 下运行 `.dbg` 文件。如果你需要调试
`` 文件本身,你可以独立加载调试符号,如下所示:
```
gdb foo -ex 'add-symbol-file foo.dbg 0x401000'
```
## 平台说明
### Shell
如果你使用 zsh 并且在运行 APE 程序时遇到问题,请尝试运行 `sh -c ./prog` 或直接升级到 zsh 5.9+(因为我们两年前就已经对其进行了修补)。对于 Python 的 `subprocess`、旧版本的 fish 等情况也是如此。
### Linux
一些 Linux 系统被配置为在 WINE 下启动 MZ 可执行文件。
其他发行版则配置了其默认安装,使得 APE 程序会
输出“run-detectors: unable to find an interpreter”。例如:
```
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
```
你可以通过在 `binfmt_misc` 中注册 APE 来解决此问题:
```
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"
```
现在应该没问题了。APE 不仅能正常工作,而且现在启动可执行文件的速度也会快 400µs。不过,如果仍然无法工作,也可以按如下方式禁用 `binfmt_misc`:
```
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
在 WSL 环境中使用 APE 通常是不安全的,因为它尝试在 WSL 环境中将 MZ 可执行文件作为 WIN32 二进制文件运行。为了在 WSL 上安全地使用 Cosmopolitan 软件,请运行以下命令:
```
sudo sh -c "echo -1 > /proc/sys/fs/binfmt_misc/WSLInterop"
```
## Discord 聊天室
Cosmopolitan 开发团队在 Redbean Discord 服务器上进行协作。
欢迎你的加入!
## 特别感谢
该项目的资金是通过
[GitHub Sponsors](https://github.com/sponsors/jart) 和
[Patreon](https://www.patreon.com/jart) 众筹的。你的支持是这个项目得以
实现的基础。感谢你!我们还要特别感谢
以下团队和个人:
- [Joe Drumgoole](https://github.com/jdrumgoole)
- [Rob Figueiredo](https://github.com/robfig)
- [Wasmer](https://wasmer.io/)
标签:客户端加密