【Cosmopolitan】x86_64架构下Windows/Linux系统通用的C语言标准库;编译出的C/C++程序可以同时在Windows与Linux系统上运行

文章介绍了如何在Linux环境下编译和安装CosmopolitanC标准库,这是一个允许C程序在多种操作系统上原生运行的库。通过编译工具脚本cosmoc++,用户可以生成能在Linux、Mac、Windows等平台上运行的二进制文件。文章详细列出了编译步骤,包括克隆项目、放宽权限、编译库和工具链,以及检查和使用新编译的C和C++编译器。
摘要由CSDN通过智能技术生成

⚠️阅读前请注意

Cosmopolitan C标准库

Cosmopolitan

项目地址: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系统上运行。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值