C++-compatible definitions of C99-style inline functions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,

I have some 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 using the C99 extern inline.

However, when I try to use one of the inline functions 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 extern "C" specifier.

If I #ifdef __cplusplus a definition with the gnu_inline attribute,
the right thing /seems/ to happen and no symbol is generated by g++,
but I'm a bit uncomfortable with this approach; it's not portable, and
I'm not sure I understand what's happening in the C++ optimizer.

Is there a portable replacement for the gnu_inline attribute in this case?
How does this affect what g++ actually optimizes?

Thanks.

Here is an example:

// buffer.h

#ifndef BUFF_H
#define BUFF_H

#include <stdbool.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

#if defined(__cplusplus) && 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);
}

#ifdef __cplusplus
}
#endif

#endif /* BUFF_H */


// buffer.c
#include "buffer.h"

extern inline bool has_remaining(void const* const obj);


// app.cpp
#include <stdlib.h>

#include "buffer.h

int main(int argc, char** argv) {
   char const* str = "okay";
   has_remaining(str);
   return (0);
}

# Compile with gcc 4.7.2 / mingw32

$ gcc -g -O0 -std=gnu99 -o buffer.o -c buffer.c
$ g++ -g -O0 -std=gnu++11 -o app.o -c app.cpp
$ g++ -Wl,--subsystem,console -o app.exe app.o buffer.o
buffer.o: In function `has_remaining':
c:\tmp/buffer.h:17: multiple definition of `has_remaining'
app.o:c:\tmp/buffer.h:17: first defined here
collect2.exe: error: ld returned 1 exit status


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux