查查知识网

cosmopolitan 时尚芭莎官网

发布者:陈阳一
导读cosmopolitan库logofast portable static native textmode containers,快速可移植的静态本地文本模式容器,这是github的简介。Cosmop

cosmopolitan库logo

fast portable static native textmode containers,快速可移植的静态本地文本模式容器,这是github的简介。

Cosmopolitan使C成为一种构建即用即用的语言,类似于Java,只是它不需要事先安装解释器或虚拟机。Cosmo提供了与Go和Rust等高级语言相同的可移植性优势,但它并没有发明新的语言,您也不需要配置CI系统来为每个操作系统构建单独的二进制文件。Cosmopolitan关注的是通过将C与平台分离来修复它,因此它可以很好地用于编写小型unix程序,这些程序可以很容易地分发给更广泛的用户。

下面在Windows上编译可移植可执行文件,以windows10为例

  1. 下载gnu gcc 静态链接库 http://justine.lol/linux-compiler-on-windows/cross9.zip 和 cosmopolitan库 http://justine.lol/cosmopolitan/cosmopolitan.zip
  2. 分别解压,在cosmopolitan文件夹(文件夹中有ape.lds,ape.o,cosmopolitan.a,cosmopolitan.h,crt.o)下新建hello.c
// hello.c

main() {
  printf(&34;Hello World!n&34;);
}
  1. 编译运行源码
..cross9binx86_64-pc-linux-gnu-gcc.exe -g -O -o hello.com.dbg hello.c -static -fno-pie -no-pie -mno-red-zone -fno-omit-frame-pointer -nostdlib -nostdinc -Wl,--gc-sections -Wl,-z,max-page-size=0x1000 -fuse-ld=bfd -Wl,-T,ape.lds -include cosmopolitan.h crt.o ape.o cosmopolitan.a
..cross9binx86_64-pc-linux-gnu-objcopy.exe -SO binary hello.com.dbg hello.com
hello.com
  1. 编译后得到的的hello.com文件即可以在windows中运行,也能在Linux, Mac, FreeBSD, OpenBSD等系统中运行,据称也能在裸机上运行。

总结:

cosmopolitan才发布了0.2的版本,第一版v0.1 (2021-01-28),相关库还在不断完善中,但github单日1600+的star数值得强势关注,最底层的跨平台可能会颠覆软件行业。