How to use asprintf in a flex rule, when _GNU_SOURCE not set?
<p>In order to ensure that the <code>asprintf</code> function is visible in the header file <code>&lt;stdio.h&gt;</code>, I have followed the advice of the man page and put:</p>

<pre><code>#define _GNU_SOURCE
#include &lt;stdio.h&gt;
// ... uses of asprintf();
</code></pre>

<p>This works in normal C programs, although presumably only on GNU/Linux and not on the BSDs.</p>

<p>I would like to be able to use the same trick in a flex lexer, e.g.:</p>

<pre><code>%{
#define _GNU_SOURCE
#include &lt;stdio.h&gt;
%}

%%

\"[^\"].+\" {
  asprintf(&amp;(yylval.string), "%s", yytext);
  return STRING;
}

%%

// ...
</code></pre>

<p>but I get a compile warning (<code>implicit-function-declaration</code>) for <code>asprintf</code>.</p>

<p>Upon further investigation, this is because in the C file that flex generates it includes <code>&lt;stdio.h&gt;</code> before my preamble code, without defining _GNU_SOURCE first.</p>

<p>How can I write code which uses this function which</p>

<ul>
<li>compiles correctly inside flex rules, and</li>
<li>(<em>optionally</em>) also works in non-GNU environments?</li>
</ul>