This simple script compiles and runs a C++ file with a single command. It's useful for simple programs (single .cpp files that don't require linking as a part of the compilation process). I'll create a more robust version that can handle linking header/definition files if necessary.
To use the script globally, like a regular terminal command, you must add a directory containing this script to the PATH variable. The PATH variable tells the shell where to look for terminal commands.
On macOS Catalina, I made a directory /Users/justin/bin
, where I save any
*.sh
scripts. I added this directory to the PATH variable by adding this
line to ~/.zshrc
export PATH=$PATH:/Users/justin/bin
I also made two aliases in the .zshrc file:
cprdo='compile_and_run.sh --overwrite true --post_delete true'
cpr='compile_and_run.sh'
$1
the *.cpp filename (including file extension .cpp)
--overwrite
overwrite the output file, if it exists
false by default. any string in overwrite evaluates to true.
--post_delete
delete the output file after it runs (simulates python behavior)
false by default. any string in post_delete evaluates to true.
compile_and_run.sh helloworld.cpp --overwrite true --post_delete true
cprdo helloworld.cpp