Makefile 条件包含
我正在尝试编写一个需要 ALSA 或 OSS 标头的应用程序。基本上,如果 /etc/oss.conf 不存在,我想将定义传递给编译器,因为这可能意味着 soundcard.h 标头不存在(请随时纠正我,我还是个新手与 OSS 合作)。根据 OSS 文档,您可以像这样使用 include 指令:
include /etc/oss.conf
CFLAGS := -I$(OSSLIBDIR)/include/sys
一个问题。 OSS 支持是可选的,所以我想检查标头是否存在,如果存在,则将定义传递给编译器。问题是,据我所知,没有办法检查文件是否存在于 makefile 规则之外。在规则内部,如果我使用 if 语句,由于某种原因,尝试设置 CFLAGS 不会改变它:(
test: $(objects)
@if [ -f ${OSS_CONFIG} ]; then \
. ${OSS_CONFIG}; \
CFLAGS+=" -I${OSSLIBDIR} -DUSE_OSS"; \
fi
@echo ${CFLAGS}
上面只是输出 CFLAGS 的原始值,即使 ${OSS_CONFIG}
存在.) 当然,这非常丑陋,我想知道是否有更干净的方法来做到这一点。或者我处理这件事的方式会引发一场涉及小猫种族灭绝的全球灾难性事件吗?
哦,请不要告诉我使用 autoconf。
I'm trying to write an application that needs either ALSA or OSS headers. Basically, I want to pass a define to the compiler if /etc/oss.conf does not exist, since that probably means the soundcard.h header doesn't exist (feel free to correct me on that one, I'm still new to working with OSS). Per the OSS documentation, you would use the include directive like so:
include /etc/oss.conf
CFLAGS := -I$(OSSLIBDIR)/include/sys
One problem. OSS support is optional, so I'd want to check if the header exists, and if does, pass a define to the compiler. The problem is, AFAIK there is no way to check if a file exists outside of a makefile rule. Inside the rule, if I use an if statement, for some reason, trying to set CFLAGS doesn't alter it:
test: $(objects)
@if [ -f ${OSS_CONFIG} ]; then \
. ${OSS_CONFIG}; \
CFLAGS+=" -I${OSSLIBDIR} -DUSE_OSS"; \
fi
@echo ${CFLAGS}
(The above just outputs the original value of CFLAGS, even if ${OSS_CONFIG}
exists.) This is, of course, extremely ugly, and I'm wondering if there's a cleaner way to do it. Or is the way I'm going about this going to trigger a worldwide cataclysmic event involving the genocide of kittens?
Oh, and please don't tell me to use autoconf.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个建议:使用
-include
(这样它就不会在失败时发出警告并退出)。不过,我不知道我自己是否完全掌握了这种语法。另一个黑客可能是这样的:
DUMMY_VAR := $(shell ... )
来执行任意代码。我认为这更不可能奏效。除此之外,我认为这是可能的。当我最近研究类似的问题时,我发现我无法让 make 在 makefile 创建过程中运行任意 shell 命令。
One suggest: use
-include
(then it won't warn + quit on failure). I don't know if I ever quite got this syntax to work myself, though.Another hack could be something along the lines of:
DUMMY_VAR := $(shell ... )
to execute arbitrary code. I think this is even less likely to work.Other than that, I don' think this is possible. When I looked into a similar problem recently, I found that I can't get make to run arbitrary shell commands during the makefile creation process.
是的,有一种更清洁的方法。您可以使用
wildcard
函数测试文件是否存在,然后使用ifeq
/endif
设置适当的CFLAGS
。如果您将与音频相关的标志粘贴到诸如AUDIO_CFLAGS
之类的内容中,那么您可以在整个 Makefile 中自动包含正确的标志集。它看起来像这样:
Yes, there is a cleaner way. You can test for the existence of a file with the
wildcard
function, and then useifeq
/endif
to set up the appropriateCFLAGS
. If you stick the audio-related flags into something likeAUDIO_CFLAGS
then you can automatically include the correct set of flags throughout the Makefile.It'll look something like this: