简体   繁体   English

为什么GNU总是重新链接我的项目?

[英]Why does GNU make always re-link my project?

I have the following Makefile in a directory full of .cpp and .h files: 我在.cpp.h文件的目录中有以下Makefile:

CFLAGS=-g -std=c++0x -Wall -pedantic -Wextra -D __STDC_LIMIT_MACROS -D __STDC_FORMAT_MACROS -O0
CXX=g++
LDFLAGS=-lgmp -lmathsat -lz3

all: Foo.o Bar.o
        $(CXX) $(CFLAGS) -o myexe Foo.o Bar.o $(LDFLAGS)

depend: .depend

.depend: $(wildcard *.cpp)
        rm -f ./.depend
        $(CXX) $(CFLAGS) -MM $^ > ./.depend

include .depend

%.o: %.cpp
        $(CXX) $(CFLAGS) $(INCLUDE) $< -c

clean:
        rm -f *.o myexe

When I hit make , it invariably executes the last step (linking) even when none of the .o files have changed. 当我点击make ,即使没有任何.o文件发生变化,它也总是执行最后一步(链接)。 How can I prevent make from doing that? 如何阻止make这样做呢? I'd expect make to output Everything up-to-date or something similar. 我希望make输出Everything up-to-date东西或类似的东西。

I'm on a i686 GNU/Linux machine with GNU Make 3.82 and g++ version 4.8.2. 我在使用GNU Make 3.82和g ++版本4.8.2的i686 GNU / Linux机器上。

Make relinks your project because it tries to build all . 重新链接您的项目,因为它试图构建all The rule for all does not create any file named all . all for the rule不会创建任何名为all文件。 Instead it produces myexe . 相反,它产生myexe Next time you run make, it will see that there's no all , but there's a rule to build one, so it dutifully executes that rule which happens to link myexe every time you run make. 下次运行make时,它会看到没有all ,但是有一个规则来构建一个,所以它尽职尽责地执行每次运行make时链接myexe规则。

In order to fix your problem you need to change your makefile to look roughly like this: 为了解决您的问题,您需要将makefile更改为大致如下所示:

all: myexe
    echo Build done

myexe: <myexe dependencies go here>
    $(CXX) $(CFLAGS) -o myexe $(wildcard *.o) $(LDFLAGS)

Make always tries to build the top rule. 始终尝试构建顶级规则。 For you, this is all . 对你而言,这就是all Since your all rule doesn't actually make an all file it will always be run. 由于您的all规则实际上并不构成all文件,因此它将始终运行。

Your probably want your all rule to be a myexe rule and, if you want an explicit all rule, have a dependency only rule: all: myexe . 您可能希望all规则都是myexe规则,如果您想要一个明确的all规则,则只有一个依赖规则: all: myexe

(With GNU Make, you might want to explicitly declare those targets which aren't supposed to generate a real file with a .PHONY rule. eg .PHONY: all depend clean .) (使用GNU Make,您可能希望显式声明那些不应该使用.PHONY规则生成真实文件的.PHONY 。例如.PHONY: all depend clean 。)

make is a rule-based expert system. make是一个基于规则的专家系统。

You give it a heap of rules and a target (default target is the first one listed), and then it builds a complete dependency tree. 你给它一堆规则和一个目标(默认目标是列出的第一个),然后它构建一个完整的依赖树。
All parts are rebuilt iff they are non-existent resp. 如果它们不存在,则重建所有部件。 older than their dependencies, recursively. 比它们的依赖项更早,递归。

The rule you are stumbling over is this: Because the target all does not create an output file all , make invokes the non-existent-or-outdated rule. 你绊倒的规则是这样的:因为目标all都没有创建一个输出文件all ,make会调用不存在或过时的规则。

You can correct this by making the target all not do any work but instead just depend on the output file. 您可以通过使目标all不做任何工作来纠正这个问题,而只是依赖于输出文件。 Marking it .PHONY is also a good idea. 标记它.PHONY也是一个好主意。

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

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