Bash - Compile C++ File Function As I am starting to be tired of reverse-searching in history for the C++ compile command, which I'm using - details on my flags are written here, I defined the following .bash_aliases function:

function compile-cpp {

    if [ -z "$1" ]
    then
    {
        echo "Need a file to compile as argument."
    }
    else
    {
        # extract file name from path
        filename=$(basename "$1")

        # cut the extension
        filename="${filename%.*}"

        # compile
        g++ $1 -std=c++14 -Wall -Wextra -Werror -Wpedantic -pedantic-errors -o "$filename"
    }
    fi

}


The goal was to take one cpp file as argument and compile it under the same name as the source's.

So, e.g. when I call:

compile-cpp delete.cpp


I expect it to output a file named as:

delete


In the same directory.

No problems, so far, detected by me, but as I am a C++ beginner, I can't know for sure, if this command will more or less always work.

Note: I don't wish to use a Makefile yet. The projects and various examples I work with are so little and there's so many of them, I believe it would be counterproductive.

I found the following question, probably trying to achieve somewhat similar thing:

Compile and run C++ code

But since I have different demands, looking at his code, I believe it is not its duplicate, in spite of having close to the same titles.
You could just go:

export CXXFLAGS=-std=c++14 -Wall -Wextra -Werror -Wpedantic -pedantic-errors


Then at any point you want to compile:

make plop.cpp


This will build the executable plop. Use the CXXFLAGS and does NOT need a makefile.

If you want to keep your function, I would allow people to override the defaults using the environment:

function compile-cpp {

    if [ -z "$1" ]
    then
    {
        echo "Need a file to compile as argument."
    }
    else
    {
        # extract file name from path
        filename=$(basename "$1")

        # cut the extension
        filename="${filename%.*}"

        # compile
        if [ -z "${VERBOSE}" ]
        then
        {
            # Normally just want to see the file that is being compiled.
            echo ${CXX:-g++} $1 -o ${filename}
        }
        else
        {
            # But if things are going wrong then you want to see the full command LINE.
            echo ${CXX:-g++} $1 ${CXXFLAGS:-'-std=c++14 -Wall -Wextra -Werror -Wpedantic -pedantic-errors'} -o "$filename"
        }
        fi
        ${CXX:-g++} $1 ${CXXFLAGS:-'-std=c++14 -Wall -Wextra -Werror -Wpedantic -pedantic-errors'} -o "$filename"
    }
    fi

}


Here:

${CXX:-g++} means if the environment variable is empty or null use `g++`
            otherwise use the value of `${CXX}`.

Same for the `${CXXFLAGS}`


I would prefer using the make technique, as this provides you all the facilities of Make in addition to the compiler (so it checks file date stamps) and other stuff.

## Use the fortran compiler for shits and giggles
CXX=fortran compile-cpp plop.cpp
VERBOSE=1 compile-cpp plop.cpp