【问题标题】:How to compile and run both at the same time for a c++ code in linux terminal?如何在 linux 终端中同时编译和运行 c++ 代码?
【发布时间】:2020-07-14 16:33:14
【问题描述】:

我正在使用 Ubuntu(最新)。如果我的主目录中有一个test.cpp 文件,我会在终端中编写两个命令来编译和运行这个文件。

prateek332@pp-pc:~$ g++ test.cpp 
prateek332@pp-pc:~$ ./a.out

有没有办法同时编写这两个命令(或者甚至可能是更好的方法)。我使用了流水线,但它不起作用。

prateek332@pp-pc:~$ g++ test.cpp | ./a.out

这不起作用。它不会编译到 test.cpp 文件中的新更改,而是只运行文件中的旧代码。

【问题讨论】:

    标签: c++ linux g++


    【解决方案1】:

    g++ test.cpp && ./a.out 先编译,如果成功,运行代码。

    【讨论】:

      【解决方案2】:

      您可以创建一个 shell 函数,因为这是您经常会做的事情。

      ~/.bashrc 中(或任何你的 shell 配置如 ~/.zshrc)

      function cpp() { g++ $1 && ./a.out; }
      

      现在你可以输入

      cpp test.cpp
      

      你可以随意命名函数。打开一个新的 shell 窗口来加载函数(或运行source ~/.bashrc)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-06
        • 2017-06-11
        • 2015-01-09
        相关资源
        最近更新 更多