Compile and Execute C++ in One Step

Working like golang

In Golang, we are allowed to use go run main.go to compile and execute the the source file in one step. But in C++ by default, we need to compile first then perform execution.

We can crete a shell script to let C++ working like Golang.

Setp 1 .
Creating a script to a bin directory named cpprun. Such as /usr/local/bin or ~/bin.

Step 2 .
Adding the following contents to cpprun.

#!/bin/csh
g++ -std=c++11 -o /tmp/a.out $1 && /tmp/a.out

Step 3 .
Run the cpp file.

$ cpprun main.cpp