stefan
stefan

Reputation: 10355

makefile automatic src file detection and dependency generation

I have the following setup for my c++ project:

in the src-folder, there are *.cpp and corresponding *.h files and in the obj-folders I want to have my .o-files.

so far, compiling and linking is not the problem. But now I have too many .cpp and .h files to put them into the Makefile manually. So I decided to write this little instruction in the makefile:

collect_sources:
@echo "collecting source files";
@echo "SRCS = \\" > sources;
@for file in ./src/*.cpp; \
do \
    echo "$$file \\" >> sources; \
done

I also do

-include sources

at the beginning of the makefile

The resulting file sources looks fine, although there is a backslash in the last line which I don't like. But afaik it should be harmful either.

I now also need to automatically build up dependencies. In my last version in which I defined the *.cpp-files as SRCS directly in the Makefile, the following code was good:

$(SRCDIR)/%.cpp:
  @$(CXX) $(DEPFLAGS) -MT \
  "$(subst $(SRCDIR),$(OBJDIR),$(subst $(EXT_SRC),$(EXT_OBJ),$$file))" \
  $(addprefix ,$$file) >> $(DEP);
clear_dependencies:
    echo "" > $(DEP);

depend: clear_dependencies $(SRCS)

But with including the sources-file, it never reaches the upper code block.

Here are the constants defined at the top of the Makefile:

CXX = g++
CXXFLAGS = -Wall \
       -Wextra \
       -Wuninitialized \
       -Wmissing-declarations \
       -pedantic \
       -O3 \
       -p -g -pg
LDFLAGS =  -p -g -pg
DEPFLAGS = -MM

SRCDIR  = ./src
OBJDIR  = ./obj

TARGET = ./bin/my_funky_function_which_is_not_the_real_name

-include sources

OBJSTMP =   $(SRCS:.cpp=.o)
OBJS        =   $(OBJSTMP:$(SRCDIR)=$(OBJDIR))
DEP = depend.main
EXT_SRC =   .cpp
EXT_OBJ =   .o

What am I missing? Is my approach valid/feasible?

Upvotes: 2

Views: 3608

Answers (1)

Beta
Beta

Reputation: 99144

All right, you asked for it.

1: Your collect_sources:... include sources is glorious Rube Goldberg hackery. Just do this:

SRCS = $(wildcard ./src/*.cpp)

And if you want to confirm it by eye, you can do this:

$(info $(SRCS))

2:

clear_dependencies:
    echo "" > $(DEP);

Just for the sake of aesthetics, let's fix this.

clear_dependencies:
    $(RM) $(DEP);

3:

$(SRCDIR)/%.cpp:
    @$(CXX) $(DEPFLAGS) -MT \
  "$(subst $(SRCDIR),$(OBJDIR),$(subst $(EXT_SRC),$(EXT_OBJ),$$file))" \
  $(addprefix ,$$file) >> $(DEP);

This will take some work. First the immediate problem: the target is a real file which exists, and the rule has no prerequisites. Therefore the rule will not be run (I have no idea why it worked for you in a previous version, maybe something else was different). As a temporary fix I suggest we switch to a static pattern rule and PHONY targets:

.PHONY:$(SRCS)
$(SRCS) : $(SRCDIR)/%.cpp:
    @$(CXX) $(DEPFLAGS) -MT \
   ...

See if all that works, then we can tackle the big stuff.

Upvotes: 7

Related Questions