0
votes

I've got a variable in one of my makefiles that contains a list of archive files I need to generate of the following form:

/libpath/mylib1/obj/mylib1.a
/libpath/mylib2/obj/mylib2.a
/libpath/mylib3/obj/mylib3.a

and so on. I've set up a rule to create each of these, basically it scans the parent folder for .cpp files and creates .o prerequisites within the /obj subfolder (so if /libpath/mylib1 contains foo.cpp and bar.cpp then the prerequisites are /libpath/mylib1/obj/foo.o and /libpath/mylib1/obj/bar.o etc). The only way I could get this to work was with .SECONDEXPANSION: I create a list of .cpp files, strip the folder and extension, suffix it with the .o extension and then prefix it with the target folder:

%.a: $$(addprefix $$(dir $$@),$$(addsuffix .o,$$(basename $$(notdir $$(wildcard $$(dir $$@)../*.cpp)))))

This works fine but I can't help but feel I'm going about this in an overly complicated way. Is there a better/cleaner way to do stuff like this?

1

1 Answers

1
votes

I would only suggest to extract the logic into a macro, and use the latter as prerequisite. Also I would prefer patsubst function instead of notdir + basename + addsuffix, and $(@D) automatic variable instead of $(dir $@).

lib_objects = $(patsubst %.cpp,$(@D)/%.o,$(notdir $(wildcard $(@D)/../*.cpp)))

%.a: $$(lib_objects)

IMO, looks a bit better, isn't it?