Compatible definitions of inline functions for C99 and C++

元气小坏坏 提交于 2019-12-01 21:43:57

This was reported to gcc: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56066 after a discussion starting here: http://gcc.gnu.org/ml/gcc-help/2013-01/msg00152.html

On linux, gcc emits weak symbols for the inline functions, and a strong symbol for the extern inline one. At link time, the weak ones are discarded in favor of the strong one. Apparently, on windows, things are handled differently. I don't have any experience with windows, so I can't tell what happens there.

C++11 standard states (3.2.3), that:

Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program; no diagnostic required. The definition can appear explicitly in the program, it can be found in the standard or a user-defined library, or (when appropriate) it is implicitly defined (see 12.1, 12.4 and 12.8). An inline function shall be defined in every translation unit in which it is odr-used.

C++ also knows about extern+inline, but understands it as "An inline function with external linkage shall have the same address in all translation units" (7.1.2)

So extern+inline as you are using it is pure C99 feature, and there must be sufficient for you to make something like:

#ifdef __cplusplus
#define C99_PROTOTYPE(x)
#else
#define C99_PROTOTYPE(x) x
#endif

And refer in buffer.c:

// buffer.c
C99_PROTOTYPE(extern inline bool has_remaining(void * obj);)

inline function in header for C++11 is ok and should work fine without C99-style prototypes.

Using static with inline should fix 'multiple definitions' problem. Even for a compiler which can't decide on its own that it shouldn't generate symbols for 'inline'd functions.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!