The project I work on is organised in one folder with a root Makefile, and a lot of sub-project each one being a subfolder containing its own makefile.
The root Makefile invokes make into each subdirectory (make -C command). Object files are generated at the same level as the source files.
I would like to order the root make command to redirect object file generation(and retrieving) into a specified build_dir. Is there a simple way of doing this ? (Instead of modifiying every Makefiles in every sub-project).
It’s kind of a hack, but you can do this with a combination of a compiler wrapper and
vpath.Suppose we have
foo.c:and
bar.c:Putting
vpath %.o objinside the Makefile will tellmaketo look inside theobj/directory for object files. But we need to tell the compiler to write object files into theobj/directory.gcchas no such option—but we don’t have to usegcc, we can write our own compiler wrapper that callsgccwith a modified command line.cc-wrapper:It’s an ugly shell script, but all it’s doing is modifying some arguments:
-o, addobj/to the start of this oneobj/, addobj/to the startand then calling
gcc.Then the Makefile is:
Now, all your objects go in
obj/andmaketracks dependencies correctly.It’ll take some tuning to make this ready for production—you’ll probably want to rewrite the script in a comprehensible language like Python—but this should help you get started.