简体   繁体   English

Makefile-依赖项生成

[英]Makefile - Dependency generation

I am trying to create a makefile that automatically compiles and links my .cpp files into an executable via .o files. 我正在尝试创建一个makefile,该文件会自动编译.cpp文件并将其链接到通过.o文件生成的可执行文件。 What I can't get working is automated (or even manual) dependency generation. 我无法工作的是自动(甚至手动)依赖项生成。 When i uncomment the below commented code, nothing is recompiled when i run make build . 当我取消注释以下注释的代码时,运行make build不会重新编译任何内容。 All i get is make: Nothing to be done for 'build'. 我所make: Nothing to be done for 'build'.就是make: Nothing to be done for 'build'. , even if xh (or any .h file) has changed. ,即使xh(或任何.h文件)已更改。 I've been trying to learn from this question: Makefile, header dependencies , dmckee's answer, especially. 我一直在尝试从以下问题中学习: Makefile,标头依赖项 ,dmckee的答案,尤其是。 Why isn't this makefile working? 为什么这个makefile文件不起作用?

Clarification: I can compile everything, but when I modify any header file, the .cpp files that depend on it aren't updated. 澄清:我可以编译所有内容,但是当我修改任何头文件时,依赖于它的.cpp文件不会被更新。 So, if I for instance compile my entire source, then I change a #define in the header file, and then run make build , and I get Nothing to be done for 'build'. 因此,例如,如果我编译了整个源代码,那么我在头文件中更改了#define ,然后运行make build ,那么Nothing to be done for 'build'. ,我Nothing to be done for 'build'. (when I have uncommented either commented chunks of the below code). (当我未注释以下代码的任何注释块时)。

CC=gcc
CFLAGS=-O2 -Wall
LDFLAGS=-lSDL -lstdc++
SOURCES=$(wildcard *.cpp)
OBJECTS=$(patsubst %.cpp, obj/%.o,$(SOURCES))
TARGET=bin/test.bin

# Nothing happens when i uncomment the following. (automated attempt)
#depend: .depend
#
#.depend: $(SOURCES)
#   rm -f ./.depend
#   $(CC) $(CFLAGS) -MM $^ >> ./.depend;
#
#include .depend

# And nothing happens when i uncomment the following. x.cpp and x.h are files in my project. (manual attempt)
#x.o: x.cpp x.h


clean:
    rm -f $(TARGET)
    rm -f $(OBJECTS)

run: build
    ./$(TARGET)

build: $(TARGET)

$(TARGET): $(OBJECTS)
    @mkdir -p $(@D)
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@

obj/%.o: %.cpp
    @mkdir -p $(@D)
    $(CC) -c $(CFLAGS) $< -o $@

This may take a few iterations. 这可能需要几次迭代。

1) I can't reproduce your results from your first approach (and you must be clearer than "nothing happens"-- does Make actually produce no output?). 1)我无法从您的第一种方法中再现您的结果(并且您必须比“什么都没有发生”更清楚-Make实际上没有产生输出吗?)。 This: 这个:

depend: .depend

.depend: $(SOURCES)
    rm -f ./.depend
    $(CC) $(CFLAGS) -MM $^ >> ./.depend;

include .depend

seems to work as intended. 似乎按预期工作。 I suggest you try $(info sources: $(SOURCES)) to verify that that variable contains the filenames you think it does. 我建议您尝试$(info sources: $(SOURCES))来验证该变量是否包含您认为确实包含的文件名。

2) I can't reproduce your results from your second approach (and you must be clearer than "nothing happens"-- does Make actually produce no output?). 2)我无法从第二种方法重现您的结果(而且您必须比“什么都没有发生”更清楚-Make实际上没有产生任何输出吗?)。 You tried xo: x.cpp xh when the first approach was commented out, is that right? 您尝试了xo: x.cpp xh当第一种方法被注释掉时, xo: x.cpp xh是吗?

EDIT: 编辑:
Let's concentrate on xo . 让我们专注于xo Does it contain #include "xh" ? 它包含#include "xh"吗? When you uncomment the first section and make xo , does Make produce (or modify) .depend ? 当您取消注释第一部分并make xo ,Make是否会产生(或修改) .depend Is there a line in .depend that pertains to xo , and if so what is it? .depend中是否存在与xo相关的行,如果是,那是什么? If you then modify xh and then make xo , what does Make do? 如果然后修改xh然后make xo ,Make会做什么?

You resolve only one kind of dependency with $(CC) -MM . 您只能使用$(CC) -MM解决一种依赖关系。 There are various others like changed command options (eg -DDO_SOMETHING_ELSE ), or a different set of symbols exported by a library. 还有各种其他命令,例如更改的命令选项(例如-DDO_SOMETHING_ELSE ),或库导出的不同符号集。 Traditional makes offer you lots of fun debugging inconsistent executables! 传统品牌为您提供了许多有趣的调试不一致的可执行文件!

That's where makepp comes in. Dependencies are detected automatically. 这就是makepp的用处 。依赖关系会自动检测到。 It not only rebuilds targets whenever any kind of dependency warrants this. 只要有任何依赖关系,它不仅会重建目标。 It even chains everything together and builds what is needed from bottom up. 它甚至将所有内容链接在一起,并自下而上构建所需的内容。 Ie if your linker has a -lmystuff option and you have a rule to build libmystuff.so or .a, that's all it takes, it will get built in time. 也就是说,如果您的链接器具有-lmystuff选项,并且您具有构建libmystuff.so或.a的规则,仅此而已,它将及时构建。 Likewise you can include files that don't even exist yet — impossible with your solution. 同样,您可以包含尚不存在的文件-解决方案无法实现。

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

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