find - How to properly clean in a Makefile? -


for small c/c++ projects used write clean target follow:

clean:     $(rm) *.o *~ 

for bigger projects, when sources dispatched in subdirectories, src1 , src2, write

clean:     $(rm) src1/*.o src1/*~ src2/*.o src2/*~ 

with more subdirectories, becomes messy… realized use find command this:

clean:     find . -name "*.o" -exec $(rm) {} \;     find . -name "*~"  -exec $(rm) {} \; 

however saw people use find in conjunction xargs instead of using -exec , wonder why since seems work fine…

what use , why?

i know big projects, or better compatibility, should use cmake or autotools simplicity of makefile small projects.

because exec creates 1 sub-process per file being deleted. xargs, on other hand, batches them up.

it's difference between:

rm src1/a.o rm src1/b.o rm src2/c.o 

and:

rm src1/a.o src1/b.o src2/c.o 

it's not going matter unless have lot of files it's worth knowing use case.

but don't use either of methods. make each directory responsible building (and it's subordinate directories), having makefile in there. then, top level make, have like:

all:     in module1 module2 \     \         cd $i \         $(make) \         cd .. \     done 

same deal clean target. don't assume cleaning same action every single subdirectory.

by doing that, relevant actions localised. if have directory .o files third party objects me link (so don't have source), make sure clean won't delete them in relevant makefile. solution have hose them, causing angst , gnashing of teeth :-)


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -