[英]Default compiler flags with Autotools
I want to know how to set default compiler/linker/etc. 我想知道如何设置默认的编译器/链接器/等。 flags if I use Autoconf/Automake combo. 如果使用Autoconf / Automake组合,则标记。
For example, the default compiler flag is "-O2 -g" if I don't set anything. 例如,如果我未进行任何设置,则默认的编译器标志为“ -O2 -g”。 I can just override it with something else, for example if I want to debug: 我可以用其他方法覆盖它,例如,如果要调试:
./configure 'CXXFLAGS=-O0 -g'
But I find the default configuration stupid because if I enable optimization, debugging will become impossible. 但是我发现默认配置很愚蠢,因为如果启用优化,调试将变得不可能。 So the default flags should either be "-O2" or "-O0 -g", if I run configure
without arguments. 因此,如果我运行不带参数的configure
,则默认标志应为“ -O2”或“ -O0 -g”。 How do I do it? 我该怎么做?
Edit: I tried the following solutions: 编辑:我尝试了以下解决方案:
progname_CXXFLAGS=whatever
to Makefile.am. 将progname_CXXFLAGS=whatever
放入Makefile.am。 It doesn't work, because it adds the flags to the default flags instead of replacing them. 它不起作用,因为它将标志添加到默认标志,而不是替换它们。 CXXFLAGS=whatever
into configure.ac. 将CXXFLAGS=whatever
放入configure.ac。 This works, but then I can't override it later. 这可行,但是以后不能覆盖它。 According to autoconf manual (about AC_PROG_CC
): 根据autoconf手册(关于AC_PROG_CC
):
If using the GNU C compiler, set shell variable GCC to 'yes'. 如果使用GNU C编译器,则将shell变量GCC设置为“ yes”。 If output variable CFLAGS was not already set, set it to -g -O2 for the GNU C compiler (-O2 on systems where GCC does not accept -g), or -g for other compilers. 如果尚未设置输出变量CFLAGS,则对于GNU C编译器将其设置为-g -O2(在GCC不接受-g的系统上将其设置为-O2),而对于其他编译器则将其设置为-g。 If your package does not like this default, then it is acceptable to insert the line 如果您的软件包不喜欢此默认设置,则可以插入该行
: ${CFLAGS=""}
after AC_INIT and before AC_PROG_CC to select an empty default instead. 在AC_INIT之后和AC_PROG_CC之前选择一个空默认值。
Similarly, according to autoconf manual (about AC_PROG_CXX
): 同样,根据autoconf手册(关于AC_PROG_CXX
):
If using the GNU C++ compiler, set shell variable GXX to 'yes'. 如果使用GNU C ++编译器,请将外壳变量GXX设置为“是”。 If output variable CXXFLAGS was not already set, set it to -g -O2 for the GNU C++ compiler (-O2 on systems where G++ does not accept -g), or -g for other compilers. 如果尚未设置输出变量CXXFLAGS,则将其设置为-g -O2(对于GNU C ++编译器)(在G ++不接受-g的系统上为-O2),或者将其设置为其他编译器。 If your package does not like this default, then it is acceptable to insert the line 如果您的软件包不喜欢此默认设置,则可以插入该行
: ${CXXFLAGS=""}
after AC_INIT and before AC_PROG_CXX to select an empty default instead. 在AC_INIT之后和AC_PROG_CXX之前选择一个空默认值。
If you merely want to have the default flags set for yourself whenever you run configure, there are (at least) 3 good ways to do it. 如果只想在运行configure时为自己设置默认标志,则有(至少)3种好的方法。 Either set CXXFLAGS in your environment (eg in .login or .bashrc), use a CONFIG_SITE environment variable to specify a config file, or set the value you want for CXXFLAGS in $prefix/share/config.site. 在您的环境中(例如,.login或.bashrc中)设置CXXFLAGS,使用CONFIG_SITE环境变量指定配置文件,或者在$ prefix / share / config.site中设置所需的CXXFLAGS值。 If you want to set the default flags to something other than '-O2 -g' for all users of your package, then you need to change what you want because doing so violates the principal of least surprise. 如果要为包的所有用户将默认标志设置为除-O2 -g之外的其他标志,则需要更改所需的内容,因为这样做违反了最不令人惊讶的原则。 Anyone familiar with autoconf expects the default flags to be -O2 -g and you should not change that. 任何熟悉autoconf的人都希望默认标志为-O2 -g,并且您不应更改该标志。
Go with the 3rd option given above, and just do 使用上面给出的第三个选项,然后执行
$ echo 'CXXFLAGS="-O0 -g"' > /usr/local/share/config.site
(or redirect to wherever you typically set $prefix, eg $HOME/share/config.site) As an added bonus, this will set CXXFLAGS for all autoconfiscated projects that you configure, not just your own. (或重定向到通常设置$ prefix的任何位置,例如$ HOME / share / config.site)作为一项附加功能,这将为您配置的所有自动没收的项目(不仅是您自己的项目)设置CXXFLAGS。 (Assuming you set prefix appropriately. If you want a config.site to be valid for all projects regardless of prefix, then use a CONFIG_SITE setting) (假设您适当地设置了前缀。如果您希望config.site对所有项目均有效,而与前缀无关,则使用CONFIG_SITE设置)
Meanwhile I figured out how to do it. 同时,我想出了办法。 I will give an explanation to this. 我将对此进行解释。
The basic thing is that Autoconf substitutes shell variables in Makefile.in. 基本的事情是Autoconf替代了Makefile.in中的shell变量。 The question is how do I get the value of these variables? 问题是如何获得这些变量的值? The answer is that the initialization command substitutes variables that are told them at command line (like ./configure 'CXXFLAGS=-O0 -g'
), and otherwise they are substituted by whatever command defines the default (for example, CXXFLAGS is set by AC_PROG_CXX
) if they are not empty. 答案是初始化命令将替换在命令行中告诉它们的变量(例如./configure 'CXXFLAGS=-O0 -g'
),否则它们将被定义默认值的任何命令替换(例如,CXXFLAGS设置为AC_PROG_CXX
)(如果它们不为空)。 So the solution is to set our new default before AC_PROG_CXX
but after substitutions from the command lines are made. 因此解决方案是在AC_PROG_CXX
之前但在命令行替换之后设置新的默认值。 For example: 例如:
if test -z $CXXFLAGS; then
CXXFLAGS='-O2'
fi
AC_PROG_CXX
You can set target-specific defaults in the Makefile.am, or you can set the default in the configure.ac
, and that'll apply to everything you build in the project. 您可以在Makefile.am中设置特定于目标的默认值,也可以在configure.ac
设置默认值,这将应用于您在项目中构建的所有内容。
See section 4.8.1 (and 5.10.4) in the autoconf manual . 请参见autoconf手册中的4.8.1节(和5.10.4节)。
Note the remarks in 4.8.1 about not second-guessing the eventual package user: if you do want to set flags that the user shouldn't be concerned with then set them using AM_CXXFLAGS
, flags such as this that the user should be able to override should be set in CXXFLAGS
. 请注意4.8.1中有关不对最终软件包用户进行第二次猜测的说明:如果您确实要设置用户不关心的标志,则可以使用AM_CXXFLAGS
进行设置,例如这样的标志,用户应该能够覆盖率应在CXXFLAGS
设置。
But... do you really want to do this? 但是...您真的要这样做吗?
./configure CXXFLAGS='-O0 -g'
, exactly as you described. 如果确实有必要在开发过程中关闭优化,则只需按照您的描述,使用./configure CXXFLAGS='-O0 -g'
配置您的开发系统。 If your configure.ac is written correctly, that'll configure your build without optimisation whilst leaving the (good) default unchanged. 如果您的configure.ac正确编写,那么将在不优化的情况下配置您的构建,同时保持(良好)默认设置不变。 Short version: the way you're doing now is the right way. 简短版:您现在的做法是正确的方法。
Edited to add: 编辑添加:
In general, if a shell variable appears as an argument to AC_SUBST
(either explicitly or, as in the case of things like CXXFLAGS
, implicitly within some other command), then it is substituted into the output files. 通常,如果shell变量作为AC_SUBST
的参数出现(显式或在其他命令中隐式地出现(如CXXFLAGS
之类的情况)),则将其替换为输出文件。 That is, after AC_SUBST(foo)
, the value of the $foo
variable in the ./configure
script will be substituted into @foo@
instances. 也就是说,在AC_SUBST(foo)
,。 ./configure
脚本中$foo
变量的值将被替换为@foo@
实例。
Building on the answers above, I added this to configure.ac: 在以上答案的基础上,我将其添加到configure.ac:
AC_ARG_WITH(debug, [ --with-debug add the debugging module], [AC_DEFINE(WITH_DEBUG,1,0)
AC_SUBST(WITH_DEBUG,1)
CXXFLAGS="-O0 -ggdb"])
It also defines WITH_DEBUG in the AC_CONFIG_HEADERS(config.h) and adds it to the Makefile with AC_SUBST(). 它还在AC_CONFIG_HEADERS(config.h)中定义WITH_DEBUG,并使用AC_SUBST()将其添加到Makefile中。
On your Makefile.am you can define them with 在您的Makefile.am上,您可以使用
programname_CXXFLAGS=-O0 -g
Updated: 20100628 更新时间:20100628
You should try and add the CXXFLAGS in configure.in before calling AC_PROG_CXX. 在调用AC_PROG_CXX之前,应尝试在configure.in中添加CXXFLAGS。 I did not test it, but your configure.in should look something like 我没有测试,但是您的configure.in应该看起来像
AC_INIT
...
CXXFLAGS=-MY -FLAGS
...
AC_PROG_CXX
Please, let me know if this works since I am curious :-) 请让我知道这是否可行,因为我很好奇:-)
The points regarding not surprising the user are valid, but some flags are reasonable to turn on by default (eg -Wall -Wextra
), and other flags are code base specific and are sometimes needed (eg -std=gnu99
). 关于用户不足为奇的要点是有效的,但是默认情况下有一些标志是合理的(例如-Wall -Wextra
),而其他标志是特定于代码库的,有时是必需的(例如-std=gnu99
)。
The question then becomes how to do this portably. 然后,问题就变成了如何便携地执行此操作。 I personally steal the flag check macros from the libuv project. 我个人从libuv项目中窃取了标志检查宏。 To do this I add libuv-check-flags.m4
to the m4
directory of my project. 为此,我将libuv-check-flags.m4
添加到项目的m4
目录中。 I can then do the following in my configure.ac
: 然后,我可以在configure.ac
执行以下操作:
m4_include([m4/libuv-check-flags.m4])
AM_INIT_AUTOMAKE([-Wall -Werror foreign 1.11.2])
# Checks for programs.
AC_PROG_CC
CC_CHECK_CFLAGS_APPEND([-std=gnu99])
CC_CHECK_CFLAGS_APPEND([-Wall])
CC_CHECK_CFLAGS_APPEND([-Wextra])
AC_PROG_LIBTOOL
My generated configure file then produces a compiler command line of: 然后,我生成的配置文件将生成以下编译器命令行:
gcc -g -O2 -std=gnu99 -Wall -Wextra
I can still override the -g -O2
default using the solutions above, eg: 我仍然可以使用上述解决方案覆盖-g -O2
缺省值,例如:
./configure CFLAGS='-O0 -g'
Produces a command line of: 产生以下命令行:
gcc -O0 -g -std=gnu99 -Wall -Wextra
And leveraging gcc semantics I can still disable base flags if necessary. 利用gcc语义,我仍然可以根据需要禁用基本标志。 For example I can disable warnings if I really really want to with: 例如,如果我真的想使用以下命令,则可以禁用警告:
./configure CFLAGS='-Wno-all -Wno-extra'
You may say, "well what if the compiler doesn't support those flags?" 您可能会说:“如果编译器不支持这些标志怎么办?” That's why these macros are so useful and great, since they ensure compiler features are checked first, so -Wall
and -Wextra
wouldn't be added in the first place if they weren't supported. 这就是为什么这些宏如此有用且很棒的原因,因为它们确保首先检查编译器功能,因此,如果不支持-Wall
和-Wextra
,则不会首先添加它们。
libuv is one of the most portable and widely used C libraries on the planet, so I think following their lead is reasonable. libuv是地球上最可移植且使用最广泛的C库之一,因此我认为遵循它们的表述是合理的。 And while these macros are pure C specific, adapting them for use with CXXFLAGS would be trivial. 尽管这些宏是纯C特定的,但将它们修改为与CXXFLAGS配合使用将是微不足道的。
References: 参考文献:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.