Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make autotools add --std=c11 to CFLAGS

Tags:

c

autotools

c11

There is no mention of a AC_PROG_CC_C11 analogue to AC_PROG_CC_C99.

How can I get my autotools project to put --std=c11 into CFLAGS?

like image 884
fadedbee Avatar asked Feb 17 '15 09:02

fadedbee


2 Answers

Most simply by putting

CFLAGS+=" -std=c11"

into your configure.ac (in addition to AC_PROG_CC). configure.ac is a template for a shell script, so you can simply put shell code in it. In fact, all the AC_FOO_BAR m4 macros expand to shell code themselves.

Caveat: This will not check whether your compiler actually supports the -std=c11 flag. If you want to check for that, you can use use AX_CHECK_COMPILE_FLAG from the autoconf archive:

AX_CHECK_COMPILE_FLAG([-std=c11], [
  CFLAGS+=" -std=c11"
], [
  echo "C compiler cannot compile C11 code"
  exit -1
])

...although simply waiting until something fails in the compilation also works, I suppose. The error message will be nicer this way, though.

like image 181
Wintermute Avatar answered Nov 18 '22 02:11

Wintermute


AC_DEFUN([AX_CHECK_STDC],
[AX_CHECK_COMPILE_FLAG([-std=gnu11],
    [AX_APPEND_FLAG([-std=gnu11])],
    [AX_CHECK_COMPILE_FLAG([-std=c11],
        [AX_APPEND_FLAG([-std=c11])],
        [AX_CHECK_COMPILE_FLAG([-std=c99],
            [AX_APPEND_FLAG([-std=c99])],
            [AC_MSG_ERROR([C compiled does not support at least C99!])])
        ])
    ])
])

Output:

checking whether C compiler accepts -std=gnu11... no
checking whether C compiler accepts -std=c11... no
checking whether C compiler accepts -std=c99... yes

(when building using shit Microchip xc16 PIC compiler)

like image 24
Martin Avatar answered Nov 18 '22 03:11

Martin