Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

222
Views
Is there anyway to compile many files by a single instruction in Linux bash?

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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!