问题描述
我有一个有多个来源目录的项目:
src / A
/ B
/ C
每个Makefile.am包含
AM_CXXFLAGS = -fPIC -Wall -Wextra
可以避免在每个源文件夹中重复此操作?
我试图修改src / Makefile.am和configure.in,但没有成功。我想我可以使用AC_PROG_CXX全局设置编译标志,但是找不到关于如何使用这些宏的许多文档(你有任何指针这样的文档吗?)。
提前感谢
您可以做以下几件事:
Makefile.am 上包含一个通用的makefile片段:
include $(top_srcdir)/common.mk
...
bin_PROGRAMS = foo
foo_SOURCES = ...
AM_CXXFLAGS = -fpic -Wall -Wextra
到
common.mk
,并且将来更容易通过编辑此文件向所有Makefile.am
添加更多的宏或规则。
(2)另一个解决方案是在
configure.ac
中全局设置这些变量(名称configure.in
很久以前已被弃用),如:...
AC_SUBST [AM_CXXFLAGS],[-fpic -Wall -Wextra])
...
你甚至不必在你的
Makefile.am
中说什么,他们会自动继承这个全局定义。缺点是你不能轻松地选择退出(使用第一个解决方案很容易决定不包括common.mk
),依赖关系不是真的明确的第三(当他们读取Makefile.am
时,他们没有提示关于标记可能来自哪里)。
(3)第三种解决方案是做建议:覆盖<$ c $中的用户变量CXXFLAGS c> configure.ac
。我建议反对它,因为它违背了GNU构建系统的一个特性:用户可以覆盖make
-time的这个变量。例如,您可能需要键入make CXXFLAGS =' - O0 -ggdb'
,这将覆盖
CXXFLAGS
的任何定义b $ b不是AM_CXXFLAGS
中的那些)。说实话,大多数项目不能正确支持这个,因为他们玩CXXFLAGS
。
最后,提到
-fpic
,-Wall
和-Werror
不是便携式选项。
根据项目的范围,您可能需要添加对这些()最近获得的新的配置检查宏用于测试警告标志,可用于构建共享库)。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
解决方案You can do several things:
(1) One solution is to include a common makefile fragment on all your
Makefile.am
s:include $(top_srcdir)/common.mk ... bin_PROGRAMS = foo foo_SOURCES = ...
in that case you would write
AM_CXXFLAGS = -fpic -Wall -Wextra
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 :... AC_SUBST([AM_CXXFLAGS], [-fpic -Wall -Wextra]) ...
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 typemake CXXFLAGS='-O0 -ggdb'
when debugging a piece of code, and this will overwrite any definition of
CXXFLAGS
(butnot those inAM_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).这篇关于Autotools:如何设置全局编译标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!