Interactive GCC (igcc) 使用教程
igcc Interactive GCC - C/C++ REPL. 项目地址: https://gitcode.com/gh_mirrors/ig/igcc
1. 项目介绍
Interactive GCC(简称igcc)是一个为C/C++设计的交互式解释器(REPL)。它通过修改基础源文件,编译并执行这些修改,然后收集输出的标准输出和标准错误。igcc支持多行输入,允许用户一次性添加多行代码,然后只编译一次。用户可以包含各种头文件,并可以配置igcc的多个方面。
2. 项目快速启动
要使用igcc,您可以通过以下命令进行安装:
pipx install git+https://github.com/alexandru-dinu/igcc.git
或者,您也可以使用pip安装(建议使用独立的虚拟环境):
pip install git+https://github.com/alexandru-dinu/igcc.git
安装完成后,您可以通过以下命令启动igcc:
igcc
在igcc中,您可以输入C/C++代码,并且可以使用以下命令进行操作:
.h
:显示帮助信息.e
:显示最后一次编译的错误和警告.l
:列出您已经输入的代码.L
:列出传递给编译器的完整程序.r
:重做之前撤销的命令.u
:撤销上一个命令.q
:退出
3. 应用案例和最佳实践
以下是一些使用igcc的示例:
单行输入
[1]> int a = 5;
[2]> a += 2;
[3]> cout << a << endl;
7
多行输入
[1]> .m
... for (int i = 0; i < 10; i++) {
... cout << i << " ";
... }
... .m
0 1 2 3 4 5 6 7 8 9
包含头文件
[1]> #include <vector>
[2]> vector<int> xs{1,2,3};
[3]> xs.push_back(17);
[4]> .m
... for (auto x : xs) {
... cout << x << " ";
... }
... .m
1 2 3 17
链接库
igcc -lpthread
[1]> #include <pthread.h>
[2]> pthread_t thr;
[3]> const char* msg = "Hello, World!";
[4]> // 假设在某处定义了print_msg
[5]> pthread_create(&thr, NULL, print_msg, (void*) msg); pthread_join(thr, NULL);
Hello, World!
撤销和重做命令
[1]> int x = 2;
[2]> cout << x;
2
[3]> .u
撤销 `int x = 2;`
[2]> .L
#include "boilerplate.h"
int main(void) {
return 0;
}
[2]> .r
重做 `cout << x;`
2
[3]> .L
#include "boilerplate.h"
int main(void) {
cout << x;
return 0;
}
4. 典型生态项目
目前,igcc的生态系统并不广泛,但以下是一些类似的项目,它们也为编译型语言提供了REPL功能:
通过这些项目,开发者可以探索更多在编译型语言中使用REPL的方式和方法。
igcc Interactive GCC - C/C++ REPL. 项目地址: https://gitcode.com/gh_mirrors/ig/igcc