简体   繁体   English

makefile自动src文件检测和依赖项生成

[英]makefile automatic src file detection and dependency generation

I have the following setup for my c++ project: 我的c ++项目具有以下设置:

in the src-folder, there are *.cpp and corresponding *.h files and in the obj-folders I want to have my .o-files. 在src文件夹中,有* .cpp和相应的* .h文件,在obj文件夹中,我想要我的.o文件。

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. 但是现在我有太多的.cpp和.h文件无法手动将它们放入Makefile中。 So I decided to write this little instruction in the makefile: 因此,我决定在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 在makefile的开头

The resulting file sources looks fine, although there is a backslash in the last line which I don't like. 生成的文件sources看起来不错,尽管在最后一行中有一个我不喜欢的反斜杠。 But afaik it should be harmful either. 但是afaik它也应该是有害的。

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: 在我上一个直接在Makefile中将* .cpp文件定义为SRCS版本中,以下代码很不错:

$(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. 但是,通过包含sources -file,它永远不会到达上层代码块。

Here are the constants defined at the top of the Makefile: 这是在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? 我的方法有效/可行吗?

All right, you asked for it. 好吧,你要了。

1: Your collect_sources:... include sources is glorious Rube Goldberg hackery. 1:您的collect_sources:... include sources是光荣的Rube Goldberg骇客工具。 Just do this: 这样做:

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

And if you want to confirm it by eye, you can do this: 如果要通过肉眼确认,可以执行以下操作:

$(info $(SRCS))

2: 2:

clear_dependencies:
    echo "" > $(DEP);

Just for the sake of aesthetics, let's fix this. 仅出于美学考虑,让我们对其进行修复。

clear_dependencies:
    $(RM) $(DEP);

3: 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目标:

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

See if all that works, then we can tackle the big stuff. 看看所有方法是否奏效,那么我们就可以解决大问题。

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

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