⚠️阅读前请注意
- 本博客适用于Cosmopolitan Libc 2.X版本,不适用于Cosmopolitan Libc 3.X版本。
- Cosmopolitan Libc 3.X版本请参考博客https://blog.csdn.net/weixin_41973774/article/details/137540794
Cosmopolitan C标准库
项目地址:https://github.com/jart/cosmopolitan
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.
意译:
Cosmopolitan是一个跨平台的C语言标准库。它使得C语言在物理意义上真正成为一种“编译一次,到处运行”的语言。基于Cosmopolitan C标准库编译的程序,可以在 Linux + Mac + Windows + FreeBSD + OpenBSD + NetBSD + BIOS 上本地运行,并具有良好的性能和占用空间。
编译并安装Cosmopolitan C标准库与相关工具链
参考文档:https://github.com/jart/cosmopolitan/blob/master/tool/scripts/cosmoc%2B%2B
- 编译平台要求:Linux(并且安装了GCC)
- 编译结果文件:Cosmopolitan C标准库、基于该库的GCC编译工具链、基于该库的C++标准库
- Cosmopolitan项目的本地存放路径:
/opt/cosmo/
目录 - 编译权限要求:sudo权限。如果没有权限,可以参考项目README中的说明进行编译
# 查看/opt目录的访问权限代码
# 下一步我们要放宽此权限,所以先把输出的数字记录到/tmp/opt_permission临时文件
stat -c "%a" /opt > /tmp/opt_permission
# 放宽/opt目录的权限。使/opt目录对所有人可读可写可执行,但只有/opt目录创建者与root用户可以删除或改名该目录
# 如果在多人服务器上操作,应当意识到这一步带来的风险
sudo chmod 1777 /opt
cd /opt
# 克隆Cosmopolitan项目到本地/opt/cosmo/目录
git clone --depth 1 https://github.com/jart/cosmopolitan cosmo
cd cosmo
# 编译Cosmopolitan C标准库与相关工具链
build/bootstrap/make.com
# 如果编译出错,或没有任何输出,请先执行ape/apeinstall.sh命令来安装APE loader(需要sudo权限),然后再尝试编译
编译完毕后,执行下列命令,检查基于Cosmopolitan C标准库的GCC编译工具链(即下面命令中的cosmocc编译器与cosmoc++编译器)是否成功安装:
# 创建/opt/cosmos目录。cosmocc初始化时会把一些库文件安装到/opt/cosmos目录
mkdir /opt/cosmos
# 检查基于Cosmopolitan的C语言编译器cosmocc
/opt/cosmo/tool/scripts/cosmocc --version
# 参考输出
x86_64-unknown-cosmo-gcc (GCC) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# 检查基于Cosmopolitan的C++编译器cosmoc++
/opt/cosmo/tool/scripts/cosmoc++ --version
# 参考输出
x86_64-unknown-cosmo-g++ (GCC) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
至此测试完毕。如果之后不需要再对Cosmopolitan库和编译器本身进行修改,可以将/opt
目录的权限复原:
sudo chmod $(cat /tmp/opt_permission) /opt
编译示例C/C++程序
使用cosmocc编译C测试程序
我们在任意位置创建一个名叫hello.c
的测试程序:
// Simple C program to display "Hello World"
// Header file for input output functions
#include <stdio.h>
// main function -
// where the execution of program begins
int main()
{
// prints hello world
printf("Hello World");
return 0;
}
使用cosmocc编译器编译该C代码文件:
# 简单的写法
/opt/cosmo/tool/scripts/cosmocc hello.c -o hello_c.com.dbg
# 更加严谨规范的写法
# 指定了C编译器(CC)、C++编译器(CXX)与链接器(LD)的位置
CC=/opt/cosmo/tool/scripts/cosmocc \
CXX=/opt/cosmo/tool/scripts/cosmoc++ \
LD=/opt/cosmo/tool/scripts/cosmoc++ \
$($CC hello.c -o hello_c.com.dbg)
编译完毕后得到hello_c.com.dbg
文件。对其进行处理,使其在Windows系统上也能运行:
objcopy -S -O binary hello_c.com.dbg hello_c.com
最终得到的hello_c.com
文件既可以在Linux中运行,也可以在Windows系统上运行。
使用cosmoc++编译C++测试程序
我们在任意位置创建一个名叫hello.cpp
的测试程序:
// C++ program to display "Hello World"
// Header file for input output functions
#include <iostream>
using namespace std;
// Main() function: where the execution of program begins
int main()
{
// prints hello world
cout << "Hello World";
return 0;
}
使用cosmoc++编译器编译该C++代码文件:
# 简单的写法
/opt/cosmo/tool/scripts/cosmoc++ hello.cpp -o hello_cpp.com.dbg
# 更加严谨规范的写法
# 指定了C编译器(CC)、C++编译器(CXX)与链接器(LD)的位置
CC=/opt/cosmo/tool/scripts/cosmocc \
CXX=/opt/cosmo/tool/scripts/cosmoc++ \
LD=/opt/cosmo/tool/scripts/cosmoc++ \
$($CXX hello.cpp -o hello_cpp.com.dbg)
编译完毕后得到hello_cpp.com.dbg
文件。对其进行处理,使其在Windows系统上也能运行:
objcopy -S -O binary hello_cpp.com.dbg hello_cpp.com
最终得到的hello_cpp.com
文件既可以在Linux中运行,也可以在Windows系统上运行。