我怎样才能让 make 将二进制文件放在不同的位置?

发布于 2024-11-27 08:00:20 字数 548 浏览 1 评论 0原文

简短而简单的问题,但我似乎在这里遇到了写作障碍:

假设我在与用于构建程序的 makefile 相同的目录中有一个源代码文件:

confus@confusion:~/prog$ ls
binaries  includes  main.c  Makefile

How do I get make to put the binaries for my main.cbinaries 目录中?然后,在第二次运行时, make 应该像平常一样查看二进制文件是否是最新的(并且不要再次编译它)。

我的想法是这样的:

# Makefile
.PHONY: all

SOURCES := $(wildcard *.c)
TARGETS := $(subst %.c,binaries/%.o,$(SOURCES))

all:$(TARGETS)

$(TARGETS):$(SOURCES)
    ./compile "$(subst .o,.c,$(@F))" -o "$@"

Short and easy question, but I seem to have a writer's block here:

Suppose I have a source code file in the same directory as the makefile I use to build the program:

confus@confusion:~/prog$ ls
binaries  includes  main.c  Makefile

How do I get make to put the binaries for my main.c in the binaries dir? Afterwards on a second run make should see if the binary file there is up to date (and don't compile it again) just like normal.

My thought was something like this:

# Makefile
.PHONY: all

SOURCES := $(wildcard *.c)
TARGETS := $(subst %.c,binaries/%.o,$(SOURCES))

all:$(TARGETS)

$(TARGETS):$(SOURCES)
    ./compile "$(subst .o,.c,$(@F))" -o "$@"

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

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

发布评论

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

评论(1

叫思念不要吵 2024-12-04 08:00:20

不要说所有目标都依赖于所有源,而是有一个模式规则,

binaries/%.o: %.c
    ./compile ... -o $@ -c 
lt;

您可能还需要使用 vpath

修订:
你的替代品也有问题...
这个测试有效(只是为了编译单独的 .o 文件,您仍然需要链接它们,这将是一个非常简单的规则)

# Makefile
.PHONY: all

SOURCES := $(wildcard *.c)
TARGETS := $(patsubst %.c,binaries/%.o,$(SOURCES))

all:$(TARGETS)

binaries/%.o: %.c
    $(CC) -o $@ -c 
lt;

Don't say all targets depend on all sources, instead have a pattern rule

binaries/%.o: %.c
    ./compile ... -o $@ -c 
lt;

you may also need to use a vpath

Revised:
You also had a problem with your subst ...
this test worked (just for compiling individual .o files, you still need to link them, which would be a very simple rule)

# Makefile
.PHONY: all

SOURCES := $(wildcard *.c)
TARGETS := $(patsubst %.c,binaries/%.o,$(SOURCES))

all:$(TARGETS)

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