Bash - Compile C++ File Function
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