持久化 Makefile 变量

发布于 2024-10-19 10:28:30 字数 537 浏览 1 评论 0原文

我希望像这样调用我的 Makefile:

make x11 oss bin

这应该会编译文件:main.c x11.c oss.c 并将它们链接到一个可执行文件bin

调用它的另一种方法是:

make x11 alsa bin

它应该做同样的事情,但是使用 main.c x11.c alsa.c

这是我的 Makefile:

bin: $(OBJ) main.o
    $(CC) $(LDFLAGS) $(OBJ) main.o $(LDLIBS)
x11:
    set OBJ += bsd.o
oss:
    set OBJ += oss.o
alsa:
    set OBJ += alsa.o
.c.o:
    $(CC) $(CFLAGS) -c $<

但似乎 OBJ 变量的内容不是贯穿整个食谱。有什么建议吗?

I want my Makefile to be invoked like this:

make x11 oss bin

which should result in compiling the files: main.c x11.c oss.c and link them into one executable file bin.

Another way to invoke it would be:

make x11 alsa bin

which should do the same thing but with main.c x11.c alsa.c

This is my Makefile:

bin: $(OBJ) main.o
    $(CC) $(LDFLAGS) $(OBJ) main.o $(LDLIBS)
x11:
    set OBJ += bsd.o
oss:
    set OBJ += oss.o
alsa:
    set OBJ += alsa.o
.c.o:
    $(CC) $(CFLAGS) -c 
lt;

but it seems that the contents of the OBJ variable isn't persistent throughout the recipes. Any suggestions?

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

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

发布评论

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

评论(2

冷默言语 2024-10-26 10:28:30

OBJ持久的,但是你还有另一个问题:

  • Makefile首先被make完全解析。因此 $OBJ 在这个阶段仍然未设置/为空。因此,bin 目标仅获取 main.o 作为依赖项,因为在此阶段所有变量都将替换为其值。
  • 接下来,使用命令行上给定的目标更新 OBJ 的内容。但是 OBJ 不再用于 bin 目标,因为 make 已经完成了依赖关系的构建。 (Makefile 通常不会进行第二次解析)

诀窍是在解析之前填充 OBJ 变量。一个简单的解决方案是使用以下方式调用 Makefile:

make "OBJ=x11 oss" bin

并稍微修改 Makefile 以添加 .o 部分(或者只是将它们添加到 make 命令中):

bin: $(patsubst %,%.o,$(OBJ)) main.o
    $(CC) $(LDFLAGS) $(OBJ) main.o $(LDLIBS)
.c.o:
    $(CC) $(CFLAGS) -c 
lt;

注意 :我说“Makefile 通常不会被解析第二次”。 可以多次递归地使用Makefile。这是非常复杂的,可能 99% 的情况都不需要。这种“make 滥用”的一个例子是 OpenWRT 构建系统trunk/Makefile 是 Makefile 的一个示例,它本身会被多次调用。

OBJ is persistent, but you have another problem:

  • The Makefile first is completely parsed by make. And thus $OBJ still is unset/empty in this stage. Therefore, the bin target only gets main.o as dependency since all variables are replaced with their value in this stage.
  • Next, OBJ is being updated with its content using the given targets on the command line. But OBJ is not used anymore for the bin target, since make is finihed building the dependencies already. (Makefile normally is not parsed a second time)

The trick is to have the OBJ variable filled before the parsing. A simple solution is to call the Makefile using:

make "OBJ=x11 oss" bin

and modify the Makefile a bit to have the .o-part added (or just add them to the make command):

bin: $(patsubst %,%.o,$(OBJ)) main.o
    $(CC) $(LDFLAGS) $(OBJ) main.o $(LDLIBS)
.c.o:
    $(CC) $(CFLAGS) -c 
lt;

Note: I said 'Makefile normally is not parsed a second time'. It is possible to recursively use the Makefile multiple times. This is very complicated and probably not required for 99% of all cases. An example of such 'make abuse' is the OpenWRT buildsystem. trunk/Makefile is an example of a Makefile is is called multiple times by itself.

嘦怹 2024-10-26 10:28:30

这有点像黑客。

将其添加到 Makefile 的顶部:

OBJ := $(filter-out bin,${MAKECMDGOALS})

现在,当您输入 make x11 oss bin 时,make 会解析此内容:

OBJ := x11 oss

对于 make x11 alsa bin 它将看到

OBJ := x11 alsa

您可能必须删除 x11 等的 shell 命令。等人。因为 set OBJ += bsd.o 不是有效的 shell。

This is somewhat of a hack.

Add this like to the top of your Makefile:

OBJ := $(filter-out bin,${MAKECMDGOALS})

Now when you type make x11 oss bin make parses this:

OBJ := x11 oss

and for make x11 alsa bin it will see

OBJ := x11 alsa

You will probably have to remove the shell commands for x11 et. al. as set OBJ += bsd.o is not valid shell.

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