2

I have a project that uses autotools. I want to add -D_GNU_SOURCE when I compile the project. I could just hack this into the Makefile or configure script, but they get overwritten by configure.am and Makefile.am when I generate new ones before release.

Where is the right place to define this and what is the correct syntax, is there a macro I should be using?

  • possible duplicate of [Macro definitions for headers, where to put them?](http://stackoverflow.com/questions/3384741/macro-definitions-for-headers-where-to-put-them) – hivert Jul 22 '13 at 21:59
  • I think this is different. I am asking what the best practice is for defining these types of options when building a project using autotools. That other question is about what to do when using any build system. –  Jul 22 '13 at 22:02

1 Answers1

1

You can can modify CFLAGS by adding a line like this to your configure.ac file:

CFLAGS="$CFLAGS -D_GNU_SOURCE"

then regenerate your configure script (this adds to the existing CFLAGS rather than replacing it, which it what you should do - so users can specify their own CFLAGS options when compiling and your script won't overwrite them).

However, for the specific case of _GNU_SOURCE, you should instead use the builtin autoconf macro:

AC_GNU_SOURCE

Place this early in your configure.ac file, before any rules that invoke the C compiler. Note that this doesn't add -D_GNU_SOURCE to CFLAGS, though - if you're using a configuration header (set with AC_CONFIG_HEADER) then it adds a definition for _GNU_SOURCE to that, and if you're not then it adds -D_GNU_SOURCE=1 to DEFS, which you can add to CFLAGS in your Makefile.

If you're using a configuration header (which for any non-trivial autoconf project, you probably should be) then it should be included before any system headers.

caf
  • 233,326
  • 40
  • 323
  • 462
  • I added that macro to configure.ac and reran autoconf, make fails after running configure. –  Jul 22 '13 at 22:54
  • 1
    @effervescent-phantom: `AC_GNU_SOURCE` adds the definition either to `DEFS` or to your configuration header if you're using one. In the former case you need to ensure that the definitions in `DEFS` are passed to the compiler, in the latter case you need to ensure your configuration header is included in your source files prior to the system headers. – caf Jul 23 '13 at 01:16