简体   繁体   English

Makefile make re 未运行所有模式规则

[英]Makefile make re is not running all the pattern rules

I have trouble creating dependency files while using make re .我在使用make re时无法创建依赖文件。 The %.d pattern rule will be called before fclean , also only the %.o pattern rule were called by all after fclean . %.d模式规则将在fclean之前被调用,在fclean之后也all %.o模式规则被调用。 Here is the output when i use make vs make re :这是我使用makemake re时的 output:

mkdir -p srcs/depends
c++ -Wall -Werror -Wextra -Wshadow -std=c++98 -pedantic -Iincludes -MM srcs/tests.cpp -o srcs/depends/tests.d
c++ -Wall -Werror -Wextra -Wshadow -std=c++98 -pedantic -Iincludes -MM srcs/main.cpp -o srcs/depends/main.d
mkdir -p srcs/obj
c++ -Wall -Werror -Wextra -Wshadow -std=c++98 -pedantic -Iincludes -c srcs/main.cpp -o srcs/obj/main.o
c++ -Wall -Werror -Wextra -Wshadow -std=c++98 -pedantic -Iincludes -c srcs/tests.cpp -o srcs/obj/tests.o
c++ -lstdc++ srcs/obj/main.o srcs/obj/tests.o -o main

vs ( make re ) VS( make re

rm -f -rv srcs/obj srcs/depends 
srcs/obj/main.o
srcs/obj/tests.o
srcs/obj
srcs/depends/main.d
srcs/depends/tests.d
srcs/depends
rm -f main
mkdir -p srcs/obj
c++ -Wall -Werror -Wextra -Wshadow -std=c++98 -pedantic -Iincludes -c srcs/main.cpp -o srcs/obj/main.o
c++ -Wall -Werror -Wextra -Wshadow -std=c++98 -pedantic -Iincludes -c srcs/tests.cpp -o srcs/obj/tests.o
c++ -lstdc++ srcs/obj/main.o srcs/obj/tests.o -o main

My makefile:我的makefile:

NAME        = main
SRCSDIR     = srcs
SRCS        = $(wildcard $(SRCSDIR)/*.cpp)
OBJSDIR     = srcs/obj
OBJS        = $(SRCS:$(SRCSDIR)/%.cpp=$(OBJSDIR)/%.o)
DEPENDSDIR  = srcs/depends
DEPENDS     = $(SRCS:$(SRCSDIR)/%.cpp=$(DEPENDSDIR)/%.d)
CPPFLAGS    = -Wall -Werror -Wextra -Wshadow -std=c++98 -pedantic -Iincludes
DEPFLAGS    = -MM
LDFLAGS     = -lstdc++


all: $(NAME)

$(NAME): $(OBJS)
    c++ $(LDFLAGS) $^ -o $@

$(OBJSDIR)/%.o: $(SRCSDIR)/%.cpp | $(OBJSDIR)
    c++ $(CPPFLAGS) -c $< -o $@

$(DEPENDSDIR)/%.d: $(SRCSDIR)/%.cpp | $(DEPENDSDIR)
    c++ $(CPPFLAGS) $(DEPFLAGS) $< -o $@

$(OBJSDIR) $(DEPENDSDIR):
    mkdir -p $@

-include $(DEPENDS)

clean:
    $(RM) -rv $(OBJSDIR) $(DEPENDSDIR) 

fclean: clean
    $(RM) $(NAME)

re: fclean all

.PHONY: all clean fclean re

I have looked up the gnu make documentation and couldn't find any details.我查阅了 gnu make 文档,但找不到任何详细信息。 (Maybe I don't know where to look it up). (也许我不知道在哪里查找)。 Anybody knows why or are there any other cleaner solution to this?任何人都知道为什么或者有任何其他更清洁的解决方案吗? Thanks in advance提前致谢

You are using the old-style header file generation method where make builds dependency files, then re-execs itself to read in the changed dependencies.您正在使用旧式 header 文件生成方法,其中 make 构建依赖文件,然后重新执行自身以读取更改的依赖项。 So, when you run make first it parses all the .d files that currently exist (due to the include line) then it tries to rebuild the .d files, then if any have changed it re-execs, then it runs the rest of the makefile. Once it loads all the .d files and determines that they're up to date, make is done with them (for that invocation).因此,当您首先运行 make 时,它会解析当前存在的所有.d文件(由于include行),然后它会尝试重建.d文件,然后如果有任何更改,它会重新执行,然后它会运行 rest makefile。一旦它加载了所有.d文件并确定它们是最新的,make 就完成了它们(对于该调用)。 If they later get deleted, it's not going to try to recreate them.如果它们后来被删除,它不会尝试重新创建它们。 They will be recreated the next time make is run.它们将在下次运行 make 时重新创建。

In general, it's a bad idea to have a rule that runs clean and all as prerequisites.一般而言,拥有一个运行cleanall作为先决条件的规则不是一个好主意。 What if you enable parallel builds?如果启用并行构建怎么办? Now make is running the clean and all rules in parallel.现在 make 正在并行运行cleanall规则。 If you want to do this, you need to invoke them as sub-makes rather than via prerequisites, like this:如果你想这样做,你需要将它们作为 sub-makes 而不是通过先决条件来调用,就像这样:

re:
        $(MAKE) fclean
        $(MAKE) all

This will ensure each target is built serially, even with parallel builds, and that make will be started from scratch to build all .这将确保每个目标都是串行构建的,即使是并行构建,并且 make 将从头开始构建all .

Another thing you should consider is using a more modern form of dependency generation;您应该考虑的另一件事是使用更现代的依赖生成形式; for example: https://make.mad-scientist.net/papers/advanced-auto-dependency-generation/例如: https://make.mad-scientist.net/papers/advanced-auto-dependency-generation/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM