Compile and Run C++ file with single custom command

jyotirmoydotdev

Jyotirmoy Barman

Posted on November 2, 2022

Compile and Run C++ file with single custom command

I this article I am going to show how you can write your own custom command in terminal. For the purpose of explanation I am going to show how you can build any run a C++ File with just a single command .

First check if you have intsall the g++ compiler in your computer or not, to check it run this command, it will show the version of your g++ compiler. If it shows you ther version you are ready to go.

g++ --version
Enter fullscreen mode Exit fullscreen mode

Open your Terminal App in Mac, and type cd this will bring you ~ or /Users/YourUserName folder.

In this folder you need to make a new file in which you want to write the command . For this example I am giving the file name .custom_command.sh, now let us understand the file name as you can see I have given a . in-front of the file name, dot will hide the file and save from accidental delete. In place of custom_command you can give any file name and the file ends with the extension .sh which is shell script.

This command will make the file.

touch .custom_command.sh
Enter fullscreen mode Exit fullscreen mode

Now, We need to write the Script inside this file, open the file with vim.

vim .custom_command.sh
Enter fullscreen mode Exit fullscreen mode

Now press I on vim and copy the code below and paste it in the file. After you have pasted the code hit esc and write :wq and hit enter this will save the file changes.

#!/bin/bash

function run(){
        if g++ -std=c++20 $1 -o runed; then
        ./runed
        else
                echo 'Error in '$1
        fi
}
Enter fullscreen mode Exit fullscreen mode

Code Explanation

First Line #!/bin/bash says that with which we need interact, I our case we are interacting with the bash.

Second we have made a function with the name of run this is the command with which we will compile and run the c++ file at the same time. For an example we have a C++ file name print_helloworld.cpp which output HelloWorld!, first we need to compile it and then run it but with the custom command we can do the both with just one command.

run print_helloworld.cpp
Enter fullscreen mode Exit fullscreen mode

You can give your own function name, replace the run with your own desired word in the .custom_command.sh file, for an example if your replace the run with runcpp than you need to type the below code to run the C++ file.

runcpp print_helloworld.cpp
Enter fullscreen mode Exit fullscreen mode

Now Coming on the third line, as you can see the code if inside if loop ( the loop basically says if the file compile then run the compiled code or else print a error message ). We need to see the line which says g++ -std=c++20 $1 -o runed this mean that we are using a g++ compiler to compile our c++ code and we are using c++ version 20 '-std=c++20' after which we are taking an input $1 which is the file name and creating a new file -o runed in which we will have our complied code.

If the file compiled successfully then it will run the code with the line ./runned.

Back to Setup

After you have successfully made the file .custom_command.sh open ~/.bashrc with vim if you use Bash or ~/.zshrc if you use ZSH and paste the below command at the last, this will allow to run your command from any session or terminal window.

source ~/.custom_command.sh
Enter fullscreen mode Exit fullscreen mode

Notice : If you have changed the file name in which you have written the custom command then replace the .custom_command.sh with your file name.

Now hit esc and then type :wq and then hit enter. The file has been saved. Now restart you terminal.

Test The Command

run print_helloworld.cpp
Enter fullscreen mode Exit fullscreen mode

If you like the article then hit the like button and follow me on twitter for my latest update @jyotirmoydotdev.

💖 💪 🙅 🚩
jyotirmoydotdev
Jyotirmoy Barman

Posted on November 2, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related