C语言 extern 内联函数会发生什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17504316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:52:40  来源:igfitidea点击:

What happens with an extern inline function?

ccompiler-constructionheaderlinkerinline

提问by musicmatze

What happens if I define the function in my .h file as

如果我将 .h 文件中的函数定义为

extern int returnaint(void);

define it in the related .c file as

在相关的 .c 文件中将其定义为

inline int returnaint(void) {
    return 1;
}

and include the header in another .c file and use the function? When I compile the things seperatly, creating a object file for each .c file and then link them, is the inlined function included, or what happens?

并将标题包含在另一个 .c 文件中并使用该函数?当我单独编译这些东西时,为每个 .c 文件创建一个目标文件然后链接它们,是否包含内联函数,或者会发生什么?

I know the compiler can ignore inline, but what if it does not ignore it in this case?

我知道编译器可以 ignore inline,但是如果在这种情况下它不忽略它呢?

回答by Jens Gustedt

Having added the inlineto the function definition in the .cfile is just superfluous.

将 加到文件中inline的函数定义.c是多余的。

  • Your compilation unit of the .cfile sees an externdeclaration (without inline) and an inlinedefinition. Thus it emits the symbol for the function in the object file.

  • All other compilation units only see an externdeclaration, and so they can use the function without problems, if you link your final executable with the other .ofile.

  • 您的.c文件编译单元会看到一个extern声明(没有inline)和一个inline定义。因此它发出目标文件中函数的符号。

  • 所有其他编译单元只看到一个extern声明,因此如果您将最终可执行文件与其他.o文件链接,它们可以毫无问题地使用该函数。

In fact, you just have it the wrong way around. This feature is meant to be used that you have the inlinedefintion in the .hfile, visible to everybody. This definition of the function only acts as a declaration of the symbol, just as externwould, but doesn't define it.

事实上,你只是把它弄错了。此功能旨在用于您拥有文件中的inline定义.h,对所有人可见。函数的这个定义只是作为符号的声明,就像extern会那样,但没有定义它。

An externdeclaration in just one .cfile (compilation unit) then ensures such that the symbol is defined, there.

extern仅在一个.c文件(编译单元)中的声明然后确保在那里定义符号。

The terminology is a bit confusing, the inlinedefinitionacting as declaration of the symbol, and the externdeclarationacting as definition of it

术语有点混乱,inline定义充当符号的extern声明,而声明充当符号的定义

回答by Yu Hao

It won't compile. From C11 (ISO/IEC 9899:2011) §6.7.4 Function specifiers(emphasis added):

它不会编译。来自 C11 (ISO/IEC 9899:2011) §6.7.4功能说明符(强调):

Any function with internal linkage can be an inline function. For a function with external linkage, the following restrictions apply: If a function is declared with an inline function specifier, then it shall also be defined in the same translation unit.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. An inline definition does not provide an external definition for the function, and does not forbid an external definition in another translation unit. An inline definition provides an alternative to an external definition, which a translator may use to implement any call to the function in the same translation unit. It is unspecified whether a call to the function uses the inline definition or the external definition.140)

140)Since an inline definition is distinct from the corresponding external definition and from any other corresponding inline definitions in other translation units, all corresponding objects with static storage duration are also distinct in each of the definitions.

任何具有内部链接的函数都可以是内联函数。对于具有外部链接的函数,以下限制适用:如果函数是用内联函数说明符声明的,则它也应在同一翻译单元中定义。如果翻译单元中函数的所有文件范围声明都包含不带 extern 的内联函数说明符,则该翻译单元中的定义是内联定义。内联定义不为函数提供外部定义,也不禁止另一个翻译单元中的外部定义。内联定义提供了外部定义的替代方案,翻译器可以使用它来实现对同一翻译单元中的函数的任何调用。未指定对函数的调用是使用内联定义还是外部定义。140)

140)由于内联定义不同于对应的外部定义以及其他翻译单元中的任何其他对应内联定义,因此所有具有静态存储持续时间的对应对象在每个定义中也是不同的。

The other .cfile gets only the declaration of the inlinefunction from the header, but not the definition, so it's against the rule in bold font.

另一个.c文件只inline从标题中获取函数的声明,而不是定义,因此它违反了粗体规则。

EDIT:

编辑:

As @Jens Gustedt points out, my previous explanation is wrong, because in the OP's question, the function is declared as non-inline in the header file:

正如@Jens Gustedt 指出的那样,我之前的解释是错误的,因为在 OP 的问题中,该函数在头文件中被声明为非内联:

extern int returnaint(void);

So the other .cfile will treat it like a normal function.

因此,另一个.c文件会将其视为普通函数。