Monday, October 10, 2022

[SOLVED] Can I find all .c files, compile them and create a static library with them all at once?

Issue

I want to find all my c source files from parent directory, compile them and create a static library with them all at once. I tried this ar -rc libmylib.a < gcc -c < find ../ -type f -name '*.c' but it throws: bash: gcc: No such file or directory.


Solution

gcc doesn't print the object file name so you'll have to divide it up into two command lines. Example:

find .. -type f -name '*.c' -exec gcc -c {} \;
ar -rc libmylib.a *.o


Answered By - Ted Lyngmo
Answer Checked By - David Marino (WPSolving Volunteer)