用一个外部内联函数会发生什么? [英] What happens with an extern inline function?

查看:393
本文介绍了用一个外部内联函数会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在.h文件中定义我的功能

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

extern int returnaint(void);

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

, define it in the related .c file as

inline int returnaint(void) {
    return 1;
}

和包含在另一个.c文件头,并使用功能?当我编译的事情seperatly,从而为每个.c文件一个对象文件,然后链接它们,是包括内联函数,或者发生了什么?

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?

我知道,编译器可以忽略在线,但如果它不能忽略它在这种情况下?

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

推荐答案

这将无法编译。从C11(ISO / IEC 9899:2011)§6.7.4的功能符(强调):

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

具有内部链接的任何功能,可以是内联函数。与外部功能
  联动,以下限制:如果一个函数与内联函数说明符声明的,那么它也应在同一转换单元中定义如果所有的。
  文件作用域的声明中翻译单元功能包括内联函数
  没有的extern说明,然后在翻译单位的定义是内联
  定义。内联定义不为功能提供一个外部定义,
  并且不禁止在其他翻译单元外部定义。内联定义
  提供给外部的定义,其中一个转换器可以用来实现一个替代
  任何呼叫在同一翻译单元的功能。它是不确定是否将呼叫
  函数使用内联定义或外部定义。 140)

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)由于内联的定义是从相应的外部定义和从任何其他不同
  在其他翻译单元对应的行内定义,具有静态存储所有相应的对象
  持续时间,可以在每个定义的不同

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.

其他 .C 文件仅获取在线函数的声明从标题,而不是定义,所以它是针对粗体规则。

The other .c file gets only the declaration of the inline function from the header, but not the definition, so it's against the rule in bold font.

编辑:

由于@Jens Gustedt指出的那样,我的previous解释是错误的,因为在OP的问题,函数声明为在头文件非内联:

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:

extern int returnaint(void);

所以其他 .C 文件将会把它像一个正常功能。

So the other .c file will treat it like a normal function.

这篇关于用一个外部内联函数会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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