簡體   English   中英

如何在 linux 終端中同時編譯和運行 c++ 代碼?

[英]How to compile and run both at the same time for a c++ code in linux terminal?

我正在使用 Ubuntu(最新)。 如果我的主目錄中有一個test.cpp文件,我會在終端中編寫兩個命令來編譯和運行這個文件。

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

有沒有辦法同時編寫這兩個命令(甚至可能是更好的方法)。 我使用了流水線,但它不起作用。

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

這行不通。 它不會編譯為test.cpp文件中的新更改,而只是運行文件中的舊代碼。

g++ test.cpp &&./a.out先編譯,如果編譯成功,運行代碼。

您可以創建 shell function 因為這是您經常會做的事情。

~/.bashrc (或任何你的 shell 配置就像 ~/.zshrc)

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

現在你可以輸入

cpp test.cpp

您可以隨意命名 function。 打開一個新的 shell window 以加載 function (或運行source ~/.bashrc )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM