This small script generates a small Qt project and compiles / executes the target executable.

moritz@master:~$ qtrun
Usage:
 qtrun name.pro [arguments]
 qtrun gen name

qtrun

#!/bin/bash

case "$1" in
    *.)
        project_file=$(readlink -f "$1"pro)
        shift
        "$0" $project_file "$@"
        exit
        ;;

    *.pro)
        project_file=$(readlink -f "$1")
        target=$(grep 'TARGET ' "$project_file" | awk -F ' =  ' '{print $2}')
        build_dir="$HOME/tmp/qtrun/$target"
        if test ! -e "$build_dir/Makefile"; then
            mkdir -p "$build_dir" && cd "$build_dir" && qmake "$project_file"
        else
            cd "$build_dir"
        fi
        output=$(make 2>&1)
        success=$?
        if test "$success" -eq 0; then
            shift
            "$build_dir/$target" "$@"
        else
            echo -ne '\e[0;31m'
            echo "$output"
            echo -ne '\e[0m'

        fi
        ;;

    'gen')
        shift
        if test "$1" != ""; then
            echo -n '#include "'$1'.h"

int main( int argc, char** argv )
{
    Q_UNUSED(argc)
    Q_UNUSED(argv)

    return 0;
}
' > "$1.cpp"
header_upper="${1^^}_H"
            echo -n '#ifndef '$header_upper'
#define '$header_upper'
#include <QtCore>
#endif // '$header_upper'
' > "$1.h"
            echo -n 'QT          += core

TARGET      =  '$1'
CONFIG      += console
TEMPLATE    =  app

SOURCES     += '$1'.cpp
HEADERS     += '$1'.h
' > "$1.pro"
        fi

        "$0" "$1.pro" && \
        qtcreator "$1.pro" &

        ;;
    *)
        echo 'Usage:'
        echo " `basename $0` name.pro [arguments]"
        echo " `basename $0` gen name"
        ;;
esac

No comments yet.

Leave a Comment

I respect your privacy
I don't run any trackers on this site.

Please use the share-buttons or leave comments so I know what might be worth writing about.

Thank you.
Contact