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.
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:
$(OBJS) : build/%.o : %.cpp
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.
BUILD_DIR := build
SRCS := \
a.c \
b.c \
c.c \
a/a.c \
b/b.c \
c/c.c
OBJS := ${SRCS:%.c=${BUILD_DIR}/%.o}
foo: ${OBJS}
@echo Linking $@ using $?
@touch $@
${BUILD_DIR}/%.o: %.c
@mkdir -p $(dir $@)
@echo Compiling $< ...
@touch $@
${SRCS}:
@echo Creating $@
@mkdir -p $(dir $@)
@touch $@
.PHONY: clean
clean:
rm -f foo
rm -f ${OBJS}
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.
vpath %.cpp src src/b src/c
Then refer to source files without their directory name; Make will search the vpath.
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 the SRC
list.
SRC = a/a.c a/aa.c b/b.c
TARGET = done
FILES = $(notdir $(SRC) )
#make list of source paths, sort also removes duplicates
PATHS = $(sort $(dir $(SRC) ) )
BUILD_DIR = build
OBJ = $(addprefix $(BUILD_DIR)/, $(FILES:.c=.o))
DEP = $(OBJ:.o=.d)
# default target before includes
all: $(TARGET)
include $(DEP)
vpath %.c $(PATHS)
# create dummy dependency files to bootstrap the process
%.d:
echo a=1 >$@
$(BUILD_DIR)/%.o:%.c
echo $@: $< > $(patsubst %.o,%.d,$@)
echo $< >$@
$(TARGET): $(OBJ)
echo $^ > $@
.PHONY: clean
clean:
del $(BUILD_DIR)/*.o
del $(BUILD_DIR)/*.d
Sorry about the ugly echo
's but I only had my win box to test it on.
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.
GENERATED_DIRS = a b
# Dependency generator function
mkdir_deps =$(foreach dir,$(GENERATED_DIRS),$(dir)/.mkdir.done)
# Target rule to create the generated dependency.
# And create the .mkdir.done file for stop continuous recreate the dir
%/.mkdir.done: # target rule
-mkdir $(patsubst %/,%,$(dir $@))
echo $(patsubst %/,%,$(dir $@)) >$@
all: $(mkdir_deps)
@echo Begin
clean:
@echo Cleaning
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With