0

I'm new to both C++ and using terminal on MacOS, and I was wondering if there's any way to make a shortcut for compiling.

It's somewhat annoying to have to type g++ filename.cpp -o 'filename' every time I want to compile. Just curious if there's anyway I could make a short cut where I just type g++ 'filename'.

Thanks!

Obsidian
  • 3,719
  • 8
  • 17
  • 30
Carter
  • 1

1 Answers1

0

It is best to use a Makefile (or another build system) to compile your code. Maybe check out the following thread for some insight on how C++ projects are usually built: Difference between Cmake, gnu make and manually compiling

However, if you know that you'll write single file scripts, then maybe the following bash file approach from this SO thread would work:

For easy access, this was the code example provided by @Some programmer dude:

output=$(g++ $PROG_NAME 2>&1)
if [[ $? != 0 ]]; then
    # There was an error, display the error in $output
    echo -e "Error:\n$output"
else
    # Compilation successfull
    ./a.out
fi
avgJoe
  • 832
  • 7
  • 24