I have a C++ small project using GNU Make. I'd like to be able to turn the following source files:
src/
a.cpp
b/
b.cpp
c/
c.cpp
into the following output structure (I'm not concerned about duplicates at this point):
build/
a.o
b.o
c.o
So far I have the following, which unfortunately puts the .o and .d right next to each .cpp:
OBJS := $(foreach file,$(SRCS),$(file).o)
DEPS := $(patsubst %.o,%.d,$(OBJS))
sinclude $(DEPS)
$(OBJS) : %.o : %.cpp
@echo Compiling $<
$(CC) $(CC_FLAGS) $(INCS) -MMD -o $@ $<
I'm aware of the $(notdir ...) function, but at this point my efforts to use it to filter the objects has failed. Can anyone shed some light on this? It seems like fairly reasonable thing to do.
Creating the flat directory structure as initially requested.
Does use
vpath
to track down the source files, but all the bookeeping is done automagically from theSRC
list.Sorry about the ugly
echo
's but I only had my win box to test it on.Then refer to source files without their directory name; Make will search the vpath.
This is for Win32 mingw32-make. it's works. The most important part is
-mkdir $(patsubst %/,%,$(dir $@))
for win32. We need to strip the trailling / for win32.
There are at least two ways you can do this. First (and what I'd recommend) is you can add the build directory to the target names (even when using a pattern rule). For example:
Second, you can use the VPATH variable to tell make to search a different directory for prerequisites. This is probably the more commonly (over) used approach. It has at least one serious drawback, and that is if you go with it, and later run into problems with "duplicates", there's no way to solve the problem. With the former approach, you can always mirror the source directory structure underneath the build directory to avoid duplicates clashing.
Edit: My previous answer was a little short on detail, so I will expand upon it to show that this actually works as advertised. Here is a complete working example Makefile that uses the first technique described above to solve the problem. Simply paste this into a Makefile and run make -- it will do the rest and show that this does in fact work.
Edit: I can't figure out how to get SO to allow tab characters in my answer text (it replaced them with spaces). After copying and pasting this example, you'll need to convert the leading spaces in the command scripts into tabs.
In particular, note that there are source files with duplicate names (a.c and a/a.c, b.c and b/b.c, etc) and that this doesn't cause any problems. Also note there is no use of VPATH, which I recommend to avoid using due to its inherent limitations.
You might not particulary like this approach, but:
does the trick quite well in a Makefile.am. Just sayin' that you don't need to write everything by hand.