Autotools:如何设置全局编译标志
我有一个包含多个源目录的项目:
src/A
/B
/C
在每个源目录中,Makefile.am 包含
AM_CXXFLAGS = -fPIC -Wall -Wextra
如何避免在每个源文件夹中重复此操作?
我尝试修改 src/Makefile.am 和 configure.in,但没有成功。 我以为我可以使用 AC_PROG_CXX 全局设置编译标志,但找不到太多有关如何使用这些宏的文档(您有指向此类文档的指针吗?)。
提前致谢
I have a project with several sources directories :
src/A
/B
/C
In each, the Makefile.am contains
AM_CXXFLAGS = -fPIC -Wall -Wextra
How can avoid repeating this in each source folder ?
I tried to modifiy src/Makefile.am and the configure.in, but without success. I thought I could use AC_PROG_CXX to set the compilation flags globally but can't find much documentation on how to use those macro (do you have any pointer to such a documentation ?).
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以执行以下操作:
(1) 一种解决方案是在所有
Makefile.am
上包含一个通用 makefile 片段:在这种情况下,您将写入
common.mk
将来,只需编辑此文件即可更轻松地向所有Makefile.am
添加更多宏或规则。(2) 另一种解决方案是在
configure.ac
中全局设置这些变量(名称configure.in
早已被弃用),如下所示:甚至不必在
Makefile.am
中说明任何内容,它们会自动继承此全局定义。 缺点是您无法轻松选择退出(使用第一个解决方案很容易决定不包含common.mk
),并且依赖关系对于第三方人员来说并不明确(当他们阅读Makefile.am
他们没有提示标志可能来自哪里)。(3) 第三种解决方案是按照 orsogufo 建议进行:覆盖
configure.ac 中的用户变量 CXXFLAGS
。 我建议不要这样做,因为它破坏了 GNU 构建系统的功能之一:允许用户在make
时覆盖此变量。 例如,您可能想在调试一段代码时键入,这将覆盖
CXXFLAGS
的任何定义(但是不是
AM_CXXFLAGS
中的那些)。 老实说,大多数项目都无法正确支持这一点,因为它们使用了CXXFLAGS
。最后,我应该提到
-fpic
、-Wall
和-Werror
不是可移植选项。根据您的项目范围,您可能希望为这些添加配置检查(gnulib 最近获取了新的宏来测试警告标志,并且 libtool 可用于构建共享库) 。
You can do several things:
(1) One solution is to include a common makefile fragment on all your
Makefile.am
s:in that case you would write
to
common.mk
and in the future it will be easier to add more macros or rules to allMakefile.am
s by just editing this file.(2) Another solution would be to set these variables globally in your
configure.ac
(the nameconfigure.in
has been deprecated long ago), as in :Then you don't even have to say anything in your
Makefile.am
s, they automatically inherit this global definition. The drawback is that you can't opt-out easily (with the first solution it's easy to decide not to includecommon.mk
) and the dependency is not really explicit to third-party people (when they read theMakefile.am
they have no hint about where the flags may come from).(3) A third solution would be to do as orsogufo suggested: overwriting the user variable CXXFLAGS in
configure.ac
. I would advise against it, because it defeats one of the features of the GNU Build System: users are allowed to override this variable atmake
-time. For instance you may want to typewhen debugging a piece of code, and this will overwrite any definition of
CXXFLAGS
(butnot those in
AM_CXXFLAGS
). To be honest, most projects fails to support this correctly because they play tricks withCXXFLAGS
.Finally, I should mention that
-fpic
,-Wall
, and-Werror
are not portable options.Depending on the scope of your project you may want to add configure check for these (gnulib recently acquired new macros to tests for warnings flags, and libtool can be used to build shared libraries).
在configure.ac中使用EXTRA_CFLAGS
例如,对于您的情况,它是这样的:
EXTRA_CFLAGS=-fPIC -Wall -Wextra
Use EXTRA_CFLAGS in configure.ac
For example for your case it would be this:
EXTRA_CFLAGS=-fPIC -Wall -Wextra