c - How to modify makefile to compile changed source into object directory except for a list of files -


i inherited makefile uses gnu make 3.81. overly complicated, imho because not use patterns. in addition, not automatically create object file directory when needed. i've looked @ several examples , read gnu makefile manual, still not seeing should simple. there seem many ways recommended, not clear use. have 60 c files need compiled directory named obj. but, don't want 6 test programs have 'main' programs compiled directory. in list called othersrcs. i'd have c files less othersrcs compiled obj if changes in files. also, if obj directory doesn't exist, i'd create it. 'make clean' should remove directory. i've used ant java , can dependencies work, i'm not succeeding makefile. simple example helpful used sort of exclusion along pattern c files.

in simple example, c source files in current directory foo.c, bar.c, atest.c, anothertest.c. have:

othersrcs := atest.c anothertest.c 

each of $(othersrcs) separatedly compiled , linked program in current directory. remaining c source files, whatever are, compiled directory obj, shall created when required, , resulting object files linked program foobar.

makefile

allsrcs := $(wildcard *.c) othersrcs := atest.c anothertest.c foobar_srcs := $(filter-out $(othersrcs),$(allsrcs)) foobar_objs := $(addprefix obj/,$(foobar_srcs:.c=.o)) progs := foobar atest anothertest  .phony: clean  : $(progs)  obj/%.o: %.c | obj      $(compile.c) $< -o $@  obj:     mkdir -p $@   foobar: $(foobar_objs)     $(link.o) -o $@ $^ $(ldlibs)  clean:     rm -fr $(progs) obj 

the default make runs like:

$ make mkdir -p obj cc    -c foobar.c -o obj/foobar.o cc    -c foo.c -o obj/foo.o cc    -c bar.c -o obj/bar.o cc   -o foobar obj/foobar.o obj/foo.o obj/bar.o  cc     atest.c   -o atest cc     anothertest.c   -o anothertest 

and of course make foobar first 5 lines of that.

to understand key details, see 4.3 types of prerequisites , 8.2 functions string substitution , analysis in the manual. no recipes need written programs atest , anothertest in example because they're correctly built gnu make's default rules.

if going rework inherited makefile, consider rationalising source tree, e.g. @ least not having test sources in same directory application sources.


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -