Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive search in VPATH?

My C++ project has source files organized in nested subdirectories of ./src. I have a pattern rule in my makefile which compiles all of the .cpp source files into objects:

$(OBJDIR)/%.o: %.cpp makefile
    $(CXX) -c $< -o $@

Since I am using this pattern rather than writing a compilation rule for each source file, I need to tell make to look recursively through ./src for these prerequisites. Right now I have:

VPATH := $./src/:./src/folder1:./src/folder2:./src/folder3

This works, but it feels pretty inelegant and also causes bugs when I inevitably forget to add in a new folder.

Hoping someone has a better solution!

like image 750
rampatowl Avatar asked Dec 02 '25 16:12

rampatowl


1 Answers

You can automate the building of the VPATH variable like yours by searching for subdirectories and replacing spaces with colons:

space :=
space +=
VPATH := $(subst $(space),:,$(shell find src -type d))

This assumes that you have no spaces in your directories or filenames.

With this approach, it is not clear to me what you would do if two source files in two different subdirectories have the same name -- but that seems to be more related to your overall setup than to your question about the VPATH specifically.

For the $(space) variable trick, see the nifty Escaping comma and space in GNU Make blog post.

like image 183
Reinier Torenbeek Avatar answered Dec 05 '25 09:12

Reinier Torenbeek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!