Using extern to refer to the instantiation of a non-static inline function

ⅰ亾dé卋堺 提交于 2019-12-24 01:01:58

问题


Is it possible to link with an instantiated non-static inline function by declaring it extern in another file?:

inline.c:

inline int foo(void) { return 42; }
extern inline int foo(void);

main.c:

extern int foo(void);
int main(){ return foo(); }

Empirically $CC main.c inline.c (where CC is gcc, clang or tcc) works. Is it a conforming C example?


回答1:


The first question here is about the behaviour of extern inline int foo(void); in the same translation unit as the apparent inline definition.

The text from C17 6.7.4/7 is:

If all of the file scope declarations for a function in a translation unit include the inline function specifier without extern , then the definition in that translation unit is an inline definition.

The "If all..." clause does not hold for inline.c, since the second line declares the identifier with extern. So the first line is actually an external definition, not an inline definition.

Then, using extern int foo(void); in the other translation unit is fine. foo is a function with external linkage which has an external definition. There is no rule precluding this.


It would also be fine if inline.c only contained the first line, which would be an inline definition, and extern inline int foo(void); appeared in a third translation unit. foo is still a function with external linkage and having an external definition.

Any inline definition is only relevant in the same translation unit as the inline definition.



来源:https://stackoverflow.com/questions/53042003/using-extern-to-refer-to-the-instantiation-of-a-non-static-inline-function

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