title: Compatible definitions of inline functions for C99 and C++
body: <p>I have a utility library of C99 code used by C++11 application code. A few inline functions are declared in the C99 style with code explicitly generated in the translation unit like:</p>
  
  <pre><code>// buffer.h
  inline bool has_remaining(void* obj) {
  ...
  }
  
  // buffer.c
  extern inline bool has_remaining(void * obj);
  </code></pre>
  
  <p>However, when I try to use <code>has_remaining</code> in the C++ application, I get errors about multiple definitions at link time. It seems that g++ is instantiating the inline code that already exists in the library, despite the <code>extern "C"</code> <strike>header guards</strike> specifier.</p>
  
  <p>Is there a way to coerce g++ into working with this type of definition? </p>
  
  <p>It looks like if I <code>#ifdef __cplusplus</code> an extern definition with the <code>gnu_inline</code> attribute, the right thing will happen, but surely there is a more portable way to keep modern C headers compatible with modern C++?</p>
  
  <p><strong>-- Edit: Working Example --</strong></p>
  
  <p>buffer.h:</p>
  
  <pre><code>#ifndef BUFF_H
  #define BUFF_H
  
  #include &lt;stdbool.h&gt;
  #include &lt;stddef.h&gt;
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  inline bool has_remaining(void const* const obj) {
      return (obj != NULL);
  }
  
  #ifdef __cplusplus
  }
  #endif
  
  #endif /* BUFF_H */
  </code></pre>
  
  <p>buffer.c:</p>
  
  <pre><code>#include "buffer.h"
  
  extern inline bool has_remaining(void const* const obj);
  </code></pre>
  
  <p>app.cpp:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  
  #include "buffer.h"
  
  int main(int argc, char** argv) {
    char const* str = "okay";
    printf(str);
  
    has_remaining(str);
  
    return (0);
  }
  </code></pre>
  
  <p>compile:</p>
  
  <pre><code>$ gcc -std=gnu99 -o buffer.o -c buffer.c
  $ g++ -std=gnu++11 -o app.o -c app.cpp
  $ g++ -Wl,--subsystem,console -o app.exe app.o buffer.o
  
  buffer.o:buffer.c:(.text+0x0): multiple definition of `has_remaining'
  app.o:app.cpp:(.text$has_remaining[_has_remaining]+0x0): first defined here
  collect2.exe: error: ld returned 1 exit status
  </code></pre>
  
  <p><strong>--Edit 2--</strong>
  The <code>__gnu_inline__</code> attribute does indeed fix the problem of multiple definitions. I'd still like to see a (more) portable approach or some conclusive reasoning why one doesn't exist.</p>
  
  <pre><code>#if defined(__cplusplus) &amp;&amp; defined(NOTBROKEN)
  #define EXTERN_INLINE extern inline __attribute__((__gnu_inline__))
  #else
  #define EXTERN_INLINE inline
  #endif
  
  EXTERN_INLINE bool has_remaining(void const* const obj) {
    return (obj != NULL);
  }
  </code></pre>