轻量的C++命令行交互器(基于GNU g++)

张开发
2026/4/20 5:12:44 15 分钟阅读

分享文章

轻量的C++命令行交互器(基于GNU g++)
C都用过吧通常来讲它只能用完整程序来编译、运行非常麻烦。我最近写了一个叫cppsh的linux小工具可以像python的交互模式那样使用它。zhangqwerrty:~ $ cppsh C Interactive Shell CtrlD run, quit exit, F3 save 是不是非常熟悉对就是模仿python写的。下面来试试。 coutHello Cppsh!endl; --- Running --- [Output] Hello Cppsh! 为什么没有#include呢因为这是普通模式已经使用了iostream头文件和std命名空间作为预设。一般来讲预设文件在用户主目录下名称叫.cppsh_preset.cpp内容如下可以覆盖#include iostream using namespace std; int main(void) { return 0; } z还有一个配置文件叫.cppsh_config里面只有一个数字代表用户输入的代码从预设文件的第几行插入。正常的.cppsh_config里的数字是5.如果程序没有找到这两个文件它会自动创建模板。如果您不想使用预设可以在运行时使用--full来启动全模式您需要写完整的代码然后使用CtrlD运行它。普通模式也可以使用CtrlD运行代码。运行完后代码会清空等待用户的下一次输入。zhangqwerrty:~ $ cppsh --full Full C Interactive Mode CtrlD run, quit exit, F3 save #include iostream ... using namespace std; ... int main(){ ... coutIts very good!!!endl; ... return 0;} --- Running --- [Output] Its very good!!! 您可以在全模式下直接复制您粘贴而来的C代码然后使用CtrlD运行它。由于cppsh使用g编译程序您需要提前安装g及其依赖。这个程序是在Debian 12下编写的所以运行时最好使用pyhton 3.9以上的版本。它适用于所有Linux发行版、无头模式或桌面环境上的终端。可以使用cppsh -c 来运行一条或多条C代码使用cppsh -f 来运行一大段C代码。代码都需要加上双引号。由于-c或-f只要有完整的双引号就会结束输入所以如果您的程序本体带有双引号请使用交互模式。zhangqwerrty:~ $ cppsh -c int a 1; coutaendl; [Output] 1 zhangqwerrty:~ $ cppsh -f #include iostream using namespace std; int main(){ int *p NULL; int a 12; p a; coutpendl; return 0;} [Output] 0x7fc226c324 zhangqwerrty:~ $这是帮助信息zhangqwerrty:~ $ cppsh --help Usage: cppsh [options] Options: -h, --help help --full full mode --preset file preset --line num line -c code run once -f code full once zhangqwerrty:~ $工具使用python3编写代码在主页资源里。这里不适合粘贴大段代码。免费

更多文章