持久化 Makefile 变量
我希望像这样调用我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
OBJ是持久的,但是你还有另一个问题:
main.o
作为依赖项,因为在此阶段所有变量都将替换为其值。诀窍是在解析之前填充 OBJ 变量。一个简单的解决方案是使用以下方式调用 Makefile:
并稍微修改 Makefile 以添加
.o
部分(或者只是将它们添加到 make 命令中):注意 :我说“Makefile 通常不会被解析第二次”。 可以多次递归地使用Makefile。这是非常复杂的,可能 99% 的情况都不需要。这种“make 滥用”的一个例子是 OpenWRT 构建系统。 trunk/Makefile 是 Makefile 的一个示例,它本身会被多次调用。
OBJ is persistent, but you have another problem:
main.o
as dependency since all variables are replaced with their value in this stage.The trick is to have the OBJ variable filled before the parsing. A simple solution is to call the Makefile using:
and modify the Makefile a bit to have the
.o
-part added (or just add them to the make command):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.
这有点像黑客。
将其添加到 Makefile 的顶部:
现在,当您输入
make x11 oss bin
时,make 会解析此内容:对于
make x11 alsa bin
它将看到您可能必须删除 x11 等的 shell 命令。等人。因为
set OBJ += bsd.o
不是有效的 shell。This is somewhat of a hack.
Add this like to the top of your Makefile:
Now when you type
make x11 oss bin
make parses this:and for
make x11 alsa bin
it will seeYou will probably have to remove the shell commands for x11 et. al. as
set OBJ += bsd.o
is not valid shell.