为什么要“制作”?总是重建目标?

发布于 2024-08-28 22:47:46 字数 660 浏览 5 评论 0原文

我的 Makefile 是:

OBJS = b.o c.o a.o
FLAGS = -Wall -Werror
CC = gcc

test: $(OBJS)
    $(CC) $(FLAGS) $(OBJS) -o test
b.o: b.c b.h
    $(CC) $(FLAGS) -c b.c
a.o: a.c b.h c.h
    $(CC) $(FLAGS) -c a.c
c.o: c.c c.h
    $(CC) $(FLAGS) -c c.c
clean:
    rm a
    rm *.o
all: test

如果我再次执行 make,然后再次执行 make,它总是会重建“test”。为什么要这样做?

即使我这样做:make ao它也会重建...如果有帮助的话,我在linux上。

在 Windows 中,如果我将“test”更改为“test.exe”,将“-o test”更改为“-o test.exe”,则效果很好。 所以我猜由于某种原因,我的 Linux 中的“make”无法检查目录中文件的日期戳。

我修好了! .c 是在 Windows 中创建的。我在 vi 中打开了所有 .c 和 .h,并且没有执行任何操作保存更改,并且一切正常。我认为日期戳的事情已经解决了。

My Makefile is:

OBJS = b.o c.o a.o
FLAGS = -Wall -Werror
CC = gcc

test: $(OBJS)
    $(CC) $(FLAGS) $(OBJS) -o test
b.o: b.c b.h
    $(CC) $(FLAGS) -c b.c
a.o: a.c b.h c.h
    $(CC) $(FLAGS) -c a.c
c.o: c.c c.h
    $(CC) $(FLAGS) -c c.c
clean:
    rm a
    rm *.o
all: test

If I do make then make again, it always rebuilds 'test'. Why does it do this?

Even if i do: make a.o it rebuilds... Im on linux if that helps.

In windows if I change 'test' by 'test.exe' and '-o test' by '-o test.exe', it works fine.
So I guess that for some reason 'make' in my linux cant check the datestamps of my files in the directory.

I FIXED IT!
The .c were created in Windows. I opened all .c and .h in vi, and without doing nothing save changes, and all worked. I think that the datestamp thing was fixed doing this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独夜无伴 2024-09-04 22:47:46

您的第一条规则是:

test: $(OBJS)

这意味着如果名为“test”的文件不存在,则应运行该规则并创建它。由于“test”永远不存在(该规则实际上不会创建名为“test”的文件),因此它每次都会运行。您应该将“test”更改为“a”,即该规则的实际输出

为了将来参考,如果规则实际上并未在左侧创建内容,则应该将其标记为:

.PHONY: test

Your first rule is:

test: $(OBJS)

That means if a file named 'test' doesn't exist, the rule should run and create it. Since 'test' never exists (that rule doesn't actually create a file named 'test'), it runs every time. You should change 'test' to 'a', the actual output of that rule

For future reference, if a rule doesn't actually create the thing on the left-hand side, you should mark it with:

.PHONY: test
桃扇骨 2024-09-04 22:47:46

作为最后的手段,您可以在调试模式下运行 Make (make -d) 并梳理输出。在尝试之前,我建议您在“测试”规则中添加一行,看看是否存在 Make 认为需要重建测试的先决条件(并在编译命令中使用一些自动变量,作为一个很好的做法)。

test: $(OBJS)
    @echo prereqs that are newer than test: $?
    $(CC) $(FLAGS) $^ -o $@

As a last resort you can run Make in debug mode (make -d) and comb through the output. Before trying that, I suggest you add a line to the "test" rule to see if there are any prerequisites that Make thinks require the rebuilding of test (and use some automatic variables in the compile command, as a good practice).

test: $(OBJS)
    @echo prereqs that are newer than test: $?
    $(CC) $(FLAGS) $^ -o $@
维持三分热 2024-09-04 22:47:46

您需要指定 testphony target

OBJS = b.o c.o a.o
CFLAGS = -Wall -Werror
CC = gcc

.PHONY: all test clean
# 'all' and 'clean' are also phony

all: test
test: a

a: $(OBJS)
    $(CC) $(CFLAGS) $(OBJS) -o a

b.o: b.c b.h
a.o: a.c b.h c.h
c.o: c.c c.h

clean:
    rm a
    rm *.o

我将all放在第一位,因此它是默认目标(这是常见的),删除了编译命令(make已经内置了这些命令),并更改了FLAGSCFLAGS (这是 C 编译器标志的通用名称,由 make 的内置规则使用)。

You need to specify that test is a phony target:

OBJS = b.o c.o a.o
CFLAGS = -Wall -Werror
CC = gcc

.PHONY: all test clean
# 'all' and 'clean' are also phony

all: test
test: a

a: $(OBJS)
    $(CC) $(CFLAGS) $(OBJS) -o a

b.o: b.c b.h
a.o: a.c b.h c.h
c.o: c.c c.h

clean:
    rm a
    rm *.o

I've placed all first so it is the default target (which is common), removed the commands for compiling (make already has those built-in), and changed FLAGS to CFLAGS (which is the common name for C compiler flags and used by make's built-in rules).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文