Some notes on computer stuff

How to define `_GNU_SOURCE` correctly with autoconf correctly

October 16, 2013
[programming] [autoconf] [preprocessor] [c]

_XOPEN_SOURCE

dnl Request some additional extensions.  The macro makes it possible to use
dnl features of Posix that are extensions to C, as well as platform extensions
dnl not defined by Posix.
AC_USE_SYSTEM_EXTENSIONS

Mainly for historical reasons, vifm contained the following pieces of code:

#define _GNU_SOURCE /* I don't know how portable this is but it is
                     * needed in Linux for wide char function wcwidth().
                     */

/* Curses includes follow. */

Today I finally decided to remove such pieces and put the define into correct place -- config.h. config.h is automatically included in all translation units, thus all existing defines can be removed.

My first attempt was adding the following line to the configure.in script:

AC_DEFINE([_GNU_SOURCE], [], [Enable GNU extensions.])

It turned to do what I want, but looking at results from DDG I accidentally saw this macro:

AC_GNU_SOURCE

The documentation on it is available here and here.

Using AC_GNU_SOURCE seems to be a better choice because it's more universal solution. AC_DEFINE produces:

diff --git a/config.h.in b/config.h.in
index 018bc95..61a2a67 100644
--- a/config.h.in
+++ b/config.h.in
@@ -117,3 +117,6 @@
 /* Number of bits in a file offset, on hosts where this is settable. */
 #undef _FILE_OFFSET_BITS

+/* Enable GNU extensions. */
+#undef _GNU_SOURCE
+

While AC_GNU_SOURCE gives:

diff --git a/config.h.in b/config.h.in
index 018bc95..d7bed74 100644
--- a/config.h.in
+++ b/config.h.in
@@ -106,6 +106,23 @@
 /* -n option is available for cp and mv */
 #undef SUPPORT_NO_CLOBBER

+/* Enable extensions on AIX 3, Interix.  */
+#ifndef _ALL_SOURCE
+# undef _ALL_SOURCE
+#endif
+/* Enable GNU extensions on systems that have them.  */
+#ifndef _GNU_SOURCE
+# undef _GNU_SOURCE
+#endif
+/* Enable threading extensions on Solaris.  */
+#ifndef _POSIX_PTHREAD_SEMANTICS
+# undef _POSIX_PTHREAD_SEMANTICS
+#endif
+/* Enable extensions on HP NonStop.  */
+#ifndef _TANDEM_SOURCE
+# undef _TANDEM_SOURCE
+#endif
+/* Enable general extensions on Solaris.  */
+#ifndef __EXTENSIONS__
+# undef __EXTENSIONS__
+#endif
+
+
 /* Version number of package */
 #undef VERSION

@@ -123,5 +145,18 @@
 /* Define for large files, on AIX-style hosts. */
 #undef _LARGE_FILES

+/* Define to 1 if on MINIX. */
+#undef _MINIX
+
+/* Define to 2 if the system does not provide POSIX.1 features except with
+   this defined. */
+#undef _POSIX_1_SOURCE
+
+/* Define to 1 if you need to in order for `stat' and other things to work. */
+#undef _POSIX_SOURCE
+