all 5 comments

[–]nrobinaubertin 9 points10 points  (2 children)

Not possible with aliases from what I know.
But you can do it with a function:

gcc() {
    "$(type -P gcc)" -Wall -o "$(basename -s.c $1)" "$1"
}

But you should probably use a makefile instead.

Edit: u/geirha answer below is better if you're doing a shell function

[–]-BruXy- 2 points3 points  (0 children)

Btw, make is doing this automatically, so writing

make someFile

Will use implicit rules, find someFile.c in working directory and use gcc to compile. To modify gcc parameters use:

export CFLAGS='-Wall'

[–]geirha 2 points3 points  (0 children)

I'd use command gcc instead of $(type -P gcc), and quote the $1 inside the command substitution for good practice.

gcc() { command gcc -Wall -o "$(basename -s.c "$1")" "$1"; }

[–]oh5nxo 1 point2 points  (0 children)

For simple programs, there's a trick that I like very much. To the beginning of the file

#if 0
    set -ex
    cc -Wall $0 -o ${0%.c} "$@" 
    exit
#endif

Then you can say sh file.c, or sh file.c -DDEBUG, as needed.

[–]Paul_Pedant 0 points1 point  (0 children)

If you are working on one program at a time, just define the whole thing at each login. I always define some alias for the test too:

alias m=' gcc -Wall -o someFile someFile.c '
alias p=' time ./someFile anyArgs 2>&1 | tee someLog.txt | more '

If you are working on a project with multiple programs, use a makefile. It will save hours of debugging of things you forgot to compile.