Some notes on computer stuff

How to define `_GNU_SOURCE` correctly with autoconf correctly

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


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.

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 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:


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/ b/
index 018bc95..61a2a67 100644
--- a/
+++ b/
@@ -117,3 +117,6 @@
 /* Number of bits in a file offset, on hosts where this is settable. */

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

While AC_GNU_SOURCE gives:

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

+/* Enable extensions on AIX 3, Interix.  */
+#ifndef _ALL_SOURCE
+# undef _ALL_SOURCE
+/* Enable GNU extensions on systems that have them.  */
+#ifndef _GNU_SOURCE
+# undef _GNU_SOURCE
+/* Enable threading extensions on Solaris.  */
+/* Enable extensions on HP NonStop.  */
+/* Enable general extensions on Solaris.  */
+#ifndef __EXTENSIONS__
+# undef __EXTENSIONS__
 /* 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. */