cbc 编译器

2020/12/10 编译原理

cbc 编译器是《自制编译器》中实现的。由于 cbc 编译器是在 32 位机器上工作的,而现在大部分都是 64 位,即使按照官方文档进行处理,编译时也会出现各种错误,因此想到了用 docker 搭建 32 位的系统。

$ docker run -d -ti --name cbc -v /path/cbc-1.0:/var/www/html -w /var/www/html i386/ubuntu
$ docker exec -ti cbc bash
root@261c13a74c73:/var/www/html# sed -i 's/archive.ubuntu.com/mirrors.163.com/' /etc/apt/source.list
root@261c13a74c73:/var/www/html# sed -i 's/security.ubuntu.com/mirrors.163.com/' /etc/apt/source.list
root@261c13a74c73:/var/www/html# apt-get udpate
root@261c13a74c73:/var/www/html# apt-get install -y vim gcc g++ default-jdk default-jre
root@261c13a74c73:/var/www/html# cp /usr/lib/i386-linux-gnu/crt*.o /usr/lib
root@261c13a74c73:/var/www/html# cd /path/cbc-1.0
root@261c13a74c73:/var/www/html# ./install.sh
root@261c13a74c73:/var/www/html# echo 'PATH=$PATH:/usr/local/cbc/bin' >> /etc/bash.bashrc
root@261c13a74c73:/var/www/html# . /etc/bash.bashrc
root@261c13a74c73:/var/www/html# vim hello.cb
# 以下代码是在 vim 中写入
import stdio;

int main(int argc, char **argv) {
    printf("Hello, world\n");
    return 0;
}
root@261c13a74c73:/var/www/html# cbc hello.cb
root@261c13a74c73:/var/www/html# ./hello 
Hello, world

为了防止资源失效,特地将 cbc 的源码进行了备份,另外将以上操作都打包到 docker 镜像中,之后直接拉镜像即可使用。

$ docker run -d -ti --name cbc_test lwlwufeng/cbc-compiler:1.0

Search

    Table of Contents