何处将CFLAG(例如-std = gnu99)添加到autotools项目中

fad*_*bee 25 c autotools

我有一个简单的Autotools C项目(不是C++).

CFLAG(通过检查)似乎是-g -O2.

我希望所有生成的make文件也-std=gnu99附加到CFLAGs,因为我使用for (int i = 0; i < MAX; i++)和类似.

我显然可以破解Makefile,但这会被覆盖./configure.

添加(或更改)代码所需的CFLAG的正确位置在哪里(与用户可能想要更改的CFLAG相对)?

(注意,这是将一个CFLAG(例如-std = gnu99)添加到(Eclipse CDT)autotools项目中的部分重复,因为我得到了我不想要的特定于Eclipse的答案.)


@ DevSolar的回答还没有帮助.甲configure.ac文件(下面)生成configure脚本(下文).

configure.ac:

dnl Process this file with autoconf to produce a configure script.

CFLAGS="$CFLAGS -std=gnu99"
AC_PREREQ(2.59)
AC_INIT(tuntest, 1.0)


AC_CANONICAL_SYSTEM
AM_INIT_AUTOMAKE()

AC_PROG_CC

AC_CONFIG_FILES(Makefile src/Makefile)
AC_OUTPUT
Run Code Online (Sandbox Code Playgroud)

$ grep CFLAGS配置

CFLAGS
CFLAGS
To assign environment variables (e.g., CC, CFLAGS...), specify them as
  CFLAGS      C compiler flags
ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
ac_test_CFLAGS=${CFLAGS+set}
ac_save_CFLAGS=$CFLAGS
   CFLAGS="-g"
  CFLAGS=""
     CFLAGS="-g"
if test "$ac_test_CFLAGS" = set; then
  CFLAGS=$ac_save_CFLAGS
    CFLAGS="-g -O2"
    CFLAGS="-g"
    CFLAGS="-O2"
    CFLAGS=
ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
Run Code Online (Sandbox Code Playgroud)

hro*_*tyr 26

autoconf有一个宏:

刚刚放:

AC_PROG_CC_STDC
Run Code Online (Sandbox Code Playgroud)

在你AC_PROG_CC和一切都将是正确的.

特别是当你使用其他没有-std=gnu99但在默认情况下以C99模式运行的编译器时(或者有一个不同的选项hpcc的-AC99泉水).

我将使用CFLAGS的那种事.

来自文档:

-- Macro: AC_PROG_CC_STDC
If the C compiler cannot compile ISO Standard C (currently C99),
try to add an option to output variable `CC' to make it work.  If
the compiler does not support C99, fall back to supporting ANSI
C89 (ISO C90).

After calling this macro you can check whether the C compiler has
been set to accept Standard C; if not, the shell variable
`ac_cv_prog_cc_stdc' is set to `no'.

  • 如果你想要最新的标准,那么`AC_PROG_CC_STDC`就是你要走的路.如果你想明确地使用C99,那么你需要`AC_PROG_CC_C99`宏.见这里:http://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/html_node/C-Compiler.html#C-Compiler (9认同)
  • 是否有机会明确要求*非扩展*行为(`-std = c99`)?(不是OP要求的,只是出于兴趣 - 我个人并不关心GNU扩展.) (4认同)
  • 这如何与 GCC 的 `-std=c99` 与 `-std=gnu99` 配合? (2认同)