Issue
For example, in Linux, gonna compile 3 files "test1.c", "test2.c" and "test3.c" separately to "test1", "test2" and "test3", is there a way using wildcard or pipe to do it by one line bash instruction?
Solution
There's not really such a thing as a "bash instruction", and even if you say "bash command", then gcc
isn't really such a thing either because it is not intrinsically related to, or a part of bash
. It is a separate binary/program that can be run under any shell, such as sh
, ksh
, ash
, dash
or even tcsh
.
Assuming you mean one line, you can do:
gcc prog1.c -o prog1 & gcc prog2.c -o prog2 & gcc -o prog3 prog3.c
You can equally do that in a loop as William was suggesting:
for f in test?.c; do gcc "$f" -o "${f%.c}" & done
IMHO though, the easiest is with GNU Parallel:
parallel gcc {} -o {.} ::: test?.c
There are those who will rightly say that GNU Parallel is not part of POSIX, but, to be fair, you didn't mention POSIX in your question. So, if POSIX-compliance is an issue, you may choose to avoid this suggestion.
As suggested in the comments, a Makefile
would be even better, So if you create a file called Makefile
with the following contents:
all: test1 test2 test3
You can simply run:
make
and all three programs will be compiled, only if necessary, i.e. if you have changed the source file since last compiling. Or, if you want to use all those cores that you paid Intel so handsomely for, you can do them in parallel with:
make -j
Answered By - Mark Setchell Answer Checked By - Cary Denson (WPSolving Admin)