makefile automatic src file detection and dependency generation

后端 未结 1 949
星月不相逢
星月不相逢 2021-01-15 12:42

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.

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-15 12:47

    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.

    0 讨论(0)
提交回复
热议问题