Wednesday, 1 May 2013

Compiling C++ easily on Linux (Easy C++ build system)

I was developing some C++ stuff on my Linux virtual machine and wanted to create something that would make it easy for me to compile C++ code. So I wrote up some bash code and added it into .bashrc as a function. Here's the code:

function cpprun(){
        echo "Checking if output directory exists..."
        if [ ! -d "./output" ]; then
                echo "Creating output directory..."
                mkdir "./output"
                echo "Output directory created."
        elif [ -f "./output/cpprun.o" ]; then
                echo "Backing up previous output..."
                mv ./output/cpprun.o ./output/cpprun.bak.o
                echo "Backup finished."
        fi

        echo "Running g++ compile..."
        g++ -o ./output/cpprun.o *.cpp
        if [ -f "./output/cpprun.o" ]; then
                echo "Compile finished."
                echo "Running output..."
                echo "-----------------"
                ./output/cpprun.o
                echo "-----------------"
                echo "Run finished."
        else
                echo "Compile failed."
        fi
}


Add this to your .bashrc script and then you should be able to compile your C++ code by typing cpprun in the base directory.

No comments:

Post a Comment