定义在 Makefile 和 Shell 脚本中使用的变量
我想以既可以包含在 Makefile 中也可以包含在 shell 脚本中的形式分发一组构建变量(有人应该将其附加到他们的 LDFLAGS 中)。
我现在拥有的是一个文件 buildflags.conf:
LDFLAGS_EXTRA="-static -foo -bar"
我希望我的用户能够,当他们使用 makefiles 时:
include buildflags.conf
LDFLAGS+=$LDFLAGS_EXTRA
或者,当他们使用 shell 脚本时,执行如下操作:
. buildflags.conf
gcc myapp.o -o myapp $LDFLAGS_EXTRA
但这不起作用,因为bash 需要在我的 LDFLAGS_EXTRA 定义周围加上引号,而 make 不需要它们。
有人对此有解决方案吗?我不想维护多个单独的 buildflags 文件,尽管一组从单个定义文件开始并使其适合包含在不同上下文中的脚本就可以了。
I'd like to distribute a set of build variables (which someone should append to their LDFLAGS), in a form that can both be included in a Makefile and in a shell script.
What I have now is a file buildflags.conf:
LDFLAGS_EXTRA="-static -foo -bar"
I want my users to be able to, when they're using makefiles:
include buildflags.conf
LDFLAGS+=$LDFLAGS_EXTRA
Or, when they're using a shell script, do something like:
. buildflags.conf
gcc myapp.o -o myapp $LDFLAGS_EXTRA
This doesn't work however, since bash needs quotes around my LDFLAGS_EXTRA definition, while make does not want them.
Anyone with a solution for this? I don't want to maintain multiple separate buildflags files, although a set of scripts that start from a single definition file and make it suitable for inclusion in different contexts would be fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想说最简单的解决方案是在你的 make 配方中包含包含变量定义的 shell 脚本(如果你的配方很简单,这很好用):
注意变量用法中的额外
$
和事实上,这两行实际上是一个语句,;\
很重要。I'd say the easiest solution is to just include the shell script containing the variable definitions in your make recipes (this works fine if your recipes are simple):
Note the extra
$
in the variable usage and the fact that the two lines are in fact one statement, the;\
is important.Ivo 在他的评论中的解决方案让我想到了另一个同样有效并且对于 Makefile 来说更规范的解决方案:
1)定义
LDFLAGS_EXTRA
如问题中所示2)后处理列表以在 Makefile 中使用
3)Make确保仅在 Makefile 中引用
LDFLAGS_EXTRA_POST
Ivo's solution in his comment has brought me to another one that works as well and is more canonical for Makefiles:
1) Define
LDFLAGS_EXTRA
as in the question2) Post process the list for use in Makefiles
3) Make sure to only reference
LDFLAGS_EXTRA_POST
in Makefiles