使用extern引用非静态内联函数的实例化 [英] Using extern to refer to the instantiation of a non-static inline function

查看:106
本文介绍了使用extern引用非静态内联函数的实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过在另一个文件中声明extern来与实例化的非静态内联函数链接?

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(); }

凭经验$CC main.c inline.c(CC为gccclangtcc)工作.这是符合要求的C示例吗?

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

推荐答案

这里的第一个问题是关于extern inline int foo(void);在与明显的内联定义相同的翻译单元中的行为.

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

C17 6.7.4/7中的文字是:

The text from C17 6.7.4/7 is:

如果函数中的所有文件范围声明 一个翻译单元包括不带externinline函数说明符,则该翻译单元中的定义是内联定义.

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.

子句不适用于inline.c,因为第二行用extern声明了标识符.因此,第一行实际上是一个外部定义,而不是一个内联定义.

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.

然后,在另一个翻译单元中使用extern int foo(void);可以. foo是具有外部链接的函数,具有外部定义.没有排除这一点的规则.

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.

如果inline.c仅包含第一行(这是内联定义),并且extern inline int foo(void);出现在第三翻译单元中,也可以. foo仍然是具有外部链接并具有外部定义的函数.

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.

这篇关于使用extern引用非静态内联函数的实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆