Web Analytics

igcc

⭐ 209 stars Simplified Chinese by alexandru-dinu

🌐 语言

交互式GCC

tests contrib Code style: black

[!NOTE]
Andy Balaam 的原始仓库在此:https://codeberg.org/andybalaam/igcc。
在这个分支中,我做了一些重构,并尝试让它更易于使用。

[!WARNING]
这个项目是一种对 C/C++ 开发中某些场景下缩短反馈循环的临时尝试,显然并不适用于任何严肃的用途。

交互式 GCC(igcc)是一个用于 C/C++ 的读取-求值-输出循环(REPL)。它通过用户命令操作基础源文件,在每次修改后编译源代码,然后执行生成的二进制文件并收集其 stdout 和 stderr。 支持多行(块)输入,因此你可以一次性添加多行内容,并只调用编译器一次(示例见下文)。

你可以包含各种头文件。为方便起见,默认包含了一个头文件: boilerplate.h。另外,using namespace std; 默认未启用,但你可以显式添加(示例见下文)。

igcc 的各个方面都可以配置,详见 config.yaml

入门

最简单的入门方式是使用 pipx
pipx install git+https://github.com/alexandru-dinu/igcc.git
或者,您也可以使用 uv
uv tool run git+https://github.com/alexandru-dinu/igcc
对于开发人员:
git clone https://github.com/alexandru-dinu/igcc.git
cd igcc
uv sync

可用参数:

usage: igcc [-h] [-I INCLUDE [INCLUDE ...]] [-L LIBDIR [LIBDIR ...]]
            [-l LIB [LIB ...]]

options: -h, --help show this help message and exit -I INCLUDE [INCLUDE ...] Add INCLUDE to the list of directories to be searched for header files. -L LIBDIR [LIBDIR ...] Add LIBDIR to the list of directories to be searched for library files. -l LIB [LIB ...] Search the library LIB when linking.

代码将使用 GCC(g++)进行编译,并显示结果(如果有的话)。 输入 .h 获取帮助:

$ igcc
[1]> .h
.h  Show this help message
.e  Show the last compile errors/warnings
.l  List the code you have entered
.L  List the whole program as given to the compiler
.r  Redo undone command
.u  Undo previous command
.q  Quit

示例

$ igcc
[1]> int a = 5;
[2]> a += 2;
[3]> using namespace std;
[4]> cout << a << endl;
7

[5]> int b = 17; [6]> a *= b; [7]> cout << a << ", " << b << endl; 119, 17

[8]> .L #include "boilerplate.h" using namespace std;

int main(void) { int a = 5; a += 2; cout << a << endl; int b = 17; a *= b; cout << a << ", " << b << endl;

return 0; }

支持多行输入(请检查配置中的 multiline_marker)。其优点是避免多次调用编译器。

$ igcc
[1]> .m
... for (int i = 0; i < 10; i++) {
...   std::cout << i << " ";
... }
... std::cout << "\n";
... .m
0 1 2 3 4 5 6 7 8 9
你可以包含标题:

$ igcc
[1]> #include 
[2]> std::vector xs{1,2,3};
[3]> xs.push_back(17);
[4]> .m
... for (auto x : xs) {
...   std::cout << x << " ";
... }
... .m
1 2 3 17

库可以被链接:

$ igcc -lpthread
[  1]> #include 
[  2]> pthread_t thr;
[  3]> const char* msg = "Hello, World!";
[  4]> // assuming print_msg is defined somewhere
[  5]> int ret = pthread_create(&thr, NULL, print_msg, (void*) msg); pthread_join(thr, NULL);
Hello, World!
您也可以撤销命令:

$ igcc
[1]> int x = 2
 Compile error - type .e to see it

[2]> .e : In function ‘int main()’: :7:5: error: expected ‘,’ or ‘;’ before ‘return’ :5:9: warning: unused variable ‘x’ [-Wunused-variable]

[2]> .u Undone int x = 2 [1]> int x = 2; [2]> .L #include "boilerplate.h"

int main(void) { int x = 2;

return 0; }

... 或 重做 之前撤销的命令:

$ igcc
[1]> int x = 2;
[2]> std::cout << x;
2
[3]> .u
Undone std::cout << x;
[2]> .L
#include "boilerplate.h"

int main(void) { int x = 2;

return 0; } [2]> .r Redone std::cout << x; 2 [3]> .L #include "boilerplate.h"

int main(void) { int x = 2; std::cout << x;

return 0; }

未来工作

请参见 https://github.com/alexandru-dinu/igcc/issues

类似项目

致谢

--- Tranlated By Open Ai Tx | Last indexed: 2025-12-11 ---