如何在configure.ac中检查特定的gcc特性

时间:2021-07-10 19:45:09

For example, gcc 4.7 has a new feature -Wnarrowing. In configure.ac, how can I test where a feature is supported by the current gcc or not?
There's a file in gnulibc, but doesn't make much sense to me.

例如,gcc 4.7有一个新特性——w窄化。在配置。ac,如何测试当前gcc是否支持某个特性?在gnulibc有一个文件,但是对我来说没有多大意义。

3 个解决方案

#1


0  

See http://code.google.com/p/opendoom/source/browse/trunk/VisualC8/autotools/ac_c_compile_flags.m4 for an example test of this sort - this tries to compile a trivial program with the given compiler flag, and adds it to CFLAGS if it works.

请参阅http://code.google.com/p/opendoom/source/browse/trunk/VisualC8/autotools/ac_c_compile_flags.m4,了解这类测试的示例——它试图用给定的编译器标志编译一个普通的程序,如果它有效的话,将它添加到CFLAGS中。

#2


1  

The logic behind this should probably be:

这背后的逻辑应该是:

Create a correct file that should get a warning with -Wnarrowing. Verify that it gets compiled correctly. This is a sanity check.

创建一个正确的文件,该文件应该得到一个带有- w的警告。验证它是否被正确编译。这是一个完整的检查。

Then compile that same file with -Wnarrowing, and verify that it still gets compiled correctly. This makes sure you detect compilers that don't support -Wnarrowing as an option, and don't attempt to pass bogus options to them.

然后用- w收缩来编译相同的文件,并验证它仍然被正确编译。这确保您能够检测到不支持- w窄化的编译器,也不要试图通过伪选项。

Finally, compile that same file with -Werror=narrowing, and verify that it now does not get compiled correctly. If it now fails, you can be fairly certain that the compiler does indeed support -Wnarrowing. This last check is useful to detect compilers that do accept -Wnarrowing/-Werror=narrowing, but spit out a warning "ignoring unknown option -Wnarrowing". In that case, you shouldn't be passing -Wnarrowing.

最后,使用-Werror=来编译相同的文件,并验证它现在没有正确编译。如果现在失败了,您可以相当肯定编译器确实支持- w。最后的检查对于检测那些确实接受- w/ -Werror=的编译器很有用,但是给出一个警告“忽略未知选项- w”。在这种情况下,您不应该传递- w。

Optionally, you may also want to compile a file that shouldn't get a warning with -Wnarrowing with -Werror=narrowing, in case you find a compiler where -Wnarrowing is useless and -Werror=narrowing is a hard error. I cannot think of a compiler where this would be required, though.

您也可以选择编译一个不应该得到- wnarrow with -Werror=的警告的文件,以防您发现编译器中- w是无用的,-Werror=是一个硬错误。但是,我想不出一个需要这个的编译器。

Translating this to a configure check should be trivial.

将此转换为配置检查应该很简单。

#3


1  

Both gcc and clang support -W[no-]narrowing and -W[no-]error=narrowing options.

gcc和clang都支持- w [no-]窄化,- w [no-]error=窄化选项。

With -std=c++11, gcc emits a warning by default, and clang emits an error by default. Even though you only mention gcc, I think you could extend the functionality check to compilers like clang that attempt to provide the same options and extensions. That might include Intel's icc too.

使用-std=c++11, gcc在默认情况下发出警告,clang在默认情况下发出错误。尽管您只提到gcc,但我认为您可以将功能检查扩展到像clang这样试图提供相同选项和扩展的编译器。这也可能包括英特尔的icc。

Let's assume you've selected the C++ compiler with AC_PROG_CXX, and have ensured that it's using the C++11 standard.

假设您已经使用AC_PROG_CXX选择了c++编译器,并确保它使用了c++ +11标准。

ac_save_CXXFLAGS="$CXXFLAGS"
CXXFLAGS="$CXXFLAGS -Werror -Wno-error=narrowing"
AC_LANG_PUSH([C++])

AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],
  [[int i {1.0}; (void) i;]])],
  [ac_cxx_warn_narrowing=1], [ac_cxx_warn_narrowing=0])

AS_IF([test $ac_cxx_warn_narrowing -ne 0],
  [AC_MSG_RESULT(['$CXX' supports -Wnarrowing])])

AC_LANG_POP([C++])
CXXFLAGS="$ac_save_CXXFLAGS"

Compilation will only succeed if: 1) the compiler supports -Wnarrowing related options, which implies it supports -Werror, and: 2) recognizes C++11 initialization syntax.

编译只会成功:1)编译器支持- wreduce相关选项,这意味着它支持-Werror,并且:2)识别c++ 11初始化语法。

Normally, configure.ac scripts and flags passed to configure should avoid -Werror, as it breaks too many internal tests. In this context, we ensure there are no other warnings besides the narrowing, which is why (void) i; is needed to prevent a warning about unused variables.

正常情况下,配置。传递给configure的交流脚本和标志应该避免-Werror,因为它破坏了太多的内部测试。在这种情况下,我们确保除了变窄之外没有其他警告,这就是为什么(void) i;需要防止对未使用变量的警告。

#1


0  

See http://code.google.com/p/opendoom/source/browse/trunk/VisualC8/autotools/ac_c_compile_flags.m4 for an example test of this sort - this tries to compile a trivial program with the given compiler flag, and adds it to CFLAGS if it works.

请参阅http://code.google.com/p/opendoom/source/browse/trunk/VisualC8/autotools/ac_c_compile_flags.m4,了解这类测试的示例——它试图用给定的编译器标志编译一个普通的程序,如果它有效的话,将它添加到CFLAGS中。

#2


1  

The logic behind this should probably be:

这背后的逻辑应该是:

Create a correct file that should get a warning with -Wnarrowing. Verify that it gets compiled correctly. This is a sanity check.

创建一个正确的文件,该文件应该得到一个带有- w的警告。验证它是否被正确编译。这是一个完整的检查。

Then compile that same file with -Wnarrowing, and verify that it still gets compiled correctly. This makes sure you detect compilers that don't support -Wnarrowing as an option, and don't attempt to pass bogus options to them.

然后用- w收缩来编译相同的文件,并验证它仍然被正确编译。这确保您能够检测到不支持- w窄化的编译器,也不要试图通过伪选项。

Finally, compile that same file with -Werror=narrowing, and verify that it now does not get compiled correctly. If it now fails, you can be fairly certain that the compiler does indeed support -Wnarrowing. This last check is useful to detect compilers that do accept -Wnarrowing/-Werror=narrowing, but spit out a warning "ignoring unknown option -Wnarrowing". In that case, you shouldn't be passing -Wnarrowing.

最后,使用-Werror=来编译相同的文件,并验证它现在没有正确编译。如果现在失败了,您可以相当肯定编译器确实支持- w。最后的检查对于检测那些确实接受- w/ -Werror=的编译器很有用,但是给出一个警告“忽略未知选项- w”。在这种情况下,您不应该传递- w。

Optionally, you may also want to compile a file that shouldn't get a warning with -Wnarrowing with -Werror=narrowing, in case you find a compiler where -Wnarrowing is useless and -Werror=narrowing is a hard error. I cannot think of a compiler where this would be required, though.

您也可以选择编译一个不应该得到- wnarrow with -Werror=的警告的文件,以防您发现编译器中- w是无用的,-Werror=是一个硬错误。但是,我想不出一个需要这个的编译器。

Translating this to a configure check should be trivial.

将此转换为配置检查应该很简单。

#3


1  

Both gcc and clang support -W[no-]narrowing and -W[no-]error=narrowing options.

gcc和clang都支持- w [no-]窄化,- w [no-]error=窄化选项。

With -std=c++11, gcc emits a warning by default, and clang emits an error by default. Even though you only mention gcc, I think you could extend the functionality check to compilers like clang that attempt to provide the same options and extensions. That might include Intel's icc too.

使用-std=c++11, gcc在默认情况下发出警告,clang在默认情况下发出错误。尽管您只提到gcc,但我认为您可以将功能检查扩展到像clang这样试图提供相同选项和扩展的编译器。这也可能包括英特尔的icc。

Let's assume you've selected the C++ compiler with AC_PROG_CXX, and have ensured that it's using the C++11 standard.

假设您已经使用AC_PROG_CXX选择了c++编译器,并确保它使用了c++ +11标准。

ac_save_CXXFLAGS="$CXXFLAGS"
CXXFLAGS="$CXXFLAGS -Werror -Wno-error=narrowing"
AC_LANG_PUSH([C++])

AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],
  [[int i {1.0}; (void) i;]])],
  [ac_cxx_warn_narrowing=1], [ac_cxx_warn_narrowing=0])

AS_IF([test $ac_cxx_warn_narrowing -ne 0],
  [AC_MSG_RESULT(['$CXX' supports -Wnarrowing])])

AC_LANG_POP([C++])
CXXFLAGS="$ac_save_CXXFLAGS"

Compilation will only succeed if: 1) the compiler supports -Wnarrowing related options, which implies it supports -Werror, and: 2) recognizes C++11 initialization syntax.

编译只会成功:1)编译器支持- wreduce相关选项,这意味着它支持-Werror,并且:2)识别c++ 11初始化语法。

Normally, configure.ac scripts and flags passed to configure should avoid -Werror, as it breaks too many internal tests. In this context, we ensure there are no other warnings besides the narrowing, which is why (void) i; is needed to prevent a warning about unused variables.

正常情况下,配置。传递给configure的交流脚本和标志应该避免-Werror,因为它破坏了太多的内部测试。在这种情况下,我们确保除了变窄之外没有其他警告,这就是为什么(void) i;需要防止对未使用变量的警告。