C语言 “静态”和“静态内联”函数有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7762731/
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 09:53:50  来源:igfitidea点击:

What's the difference between "static" and "static inline" function?

cinline

提问by new_perl

IMO both make the function to have a scope of the translation unit only.

IMO 都使该功能仅具有翻译单元的范围。

What's the difference between "static" and "static inline" function?

“静态”和“静态内联”函数有什么区别?

Why should inlinebe put in a header file, not in .cfile?

为什么要inline放在头文件中,而不是放在.c文件中?

回答by littleadv

inlineinstructs the compiler to attempt toembed the function content into the calling code instead of executing an actual call.

inline指示编译器尝试将函数内容嵌入到调用代码中,而不是执行实际调用。

For small functions that are called frequently that can make a big performance difference.

对于频繁调用的小函数,可以产生很大的性能差异。

However, this is only a "hint", and the compiler may ignore it, and most compilers will try to "inline" even when the keyword is not used, as part of the optimizations, where its possible.

然而,这只是一个“提示”,编译器可能会忽略它,并且大多数编译器即使在未使用关键字时也会尝试“内联”,作为优化的一部分,在可能的情况下。

for example:

例如:

static int Inc(int i) {return i+1};
.... // some code
int i;
.... // some more code
for (i=0; i<999999; i = Inc(i)) {/*do something here*/};

This tight loop will perform a function call on each iteration, and the function content is actually significantly less than the code the compiler needs to put to perform the call. inlinewill essentially instruct the compiler to convert the code above into an equivalent of:

这种紧密的循环会在每次迭代时执行一次函数调用,而函数内容实际上明显少于编译器执行调用所需的代码。inline基本上会指示编译器将上面的代码转换为等效的:

 int i;
 ....
 for (i=0; i<999999; i = i+1) { /* do something here */};

Skipping the actual function call and return

跳过实际的函数调用并返回

Obviously this is an example to show the point, not a real piece of code.

显然,这是一个说明问题的例子,而不是一段真正的代码。

staticrefers to the scope. In C it means that the function/variable can only be used within the same translation unit.

static指范围。在 C 中,这意味着函数/变量只能在同一个翻译单元中使用。

回答by Christoph

By default, an inline definition is only valid in the current translation unit.

默认情况下,内联定义仅在当前翻译单元中有效。

If the storage class is extern, the identifier has external linkage and the inline definition also provides the external definition.

如果存储类是extern,则标识符具有外部链接,并且内联定义也提供了外部定义。

If the storage class is static, the identifier has internal linkage and the inline definition is invisible in other translation units.

如果存储类是static,则标识符具有内部链接,并且内联定义在其他翻译单元中不可见。

If the storage class is unspecified, the inline definition is only visible in the current translation unit, but the identifier still has external linkage and an external definition must be provided in a different translation unit. The compiler is free to use either the inline or the external definition if the function is called within the current translation unit.

如果未指定存储类,则内联定义仅在当前翻译单元中可见,但标识符仍具有外部链接,并且必须在不同的翻译单元中提供外部定义。如果在当前翻译单元内调用函数,编译器可以自由使用内联或外部定义。

As the compiler is free to inline (and to not inline) any function whose definition is visible in the current translation unit (and, thanks to link-time optimizations, even in different translation units, though the C standard doesn't really account for that), for most practical purposes, there's no difference between staticand static inlinefunction definitions.

由于编译器可以自由地内联(和不内联)其定义在当前翻译单元中可见的任何函数(并且,由于链接时优化,即使在不同的翻译单元中,尽管 C 标准并没有真正考虑那个),最实用的目的,有没有什么区别staticstatic inline功能定义。

The inlinespecifier (like the registerstorage class) is only a compiler hint, and the compiler is free to completely ignore it. Standards-compliant non-optimizing compilers only have to honor their side-effects, and optimizing compilers will do these optimizations with or without explicit hints.

inline说明符(如register存储类)只有一个编译器暗示,编译器可以自由地完全忽略它。符合标准的非优化编译器只需要考虑它们的副作用,优化编译器将在有或没有明确提示的情况下进行这些优化。

inlineand registerare not useless, though, as they instruct the compiler to throw errors when the programmer writes code that would make the optimizations impossible: An external inlinedefinition can't reference identifiers with internal linkage (as these would be unavailable in a different translation unit) or define modifiable local variables with static storage duration (as these wouldn't share state accross translation units), and you can't take addresses of register-qualified variables.

inline并且register并非无用,因为它们指示编译器在程序员编写使优化不可能的代码时抛出错误:外部inline定义不能引用具有内部链接的标识符(因为它们在不同的翻译单元中不可用)或者定义具有静态存储持续时间的可修改局部变量(因为它们不会跨翻译单元共享状态),并且您不能获取register-qualified 变量的地址。

Personally, I use the convention to mark staticfunction definitions within headers also inline, as the main reason for putting function definitions in header files is to make them inlinable.

就个人而言,我也使用约定static在头文件中标记函数定义inline,因为将函数定义放在头文件中的主要原因是使它们可内联。

In general, I only use static inlinefunction and static constobject definitions in addition to externdeclarations within headers.

一般来说,除了头文件中的声明之外,我只使用static inline函数和static const对象定义extern

I've never written an inlinefunction with a storage class different from static.

我从未编写inline过存储类不同于static.

回答by ony

From my experience with GCC I know that staticand static inlinediffers in a way how compiler issue warnings about unused functions. More precisely when you declare staticfunction and it isn't used in current translation unit then compiler produce warning about unused function, but you can inhibit that warning with changing it to static inline.

根据我对 GCC 的经验,我知道这一点,static并且static inline在编译器发出有关未使用函数的警告的方式上有所不同。更准确地说,当您声明static函数并且它未在当前翻译单元中使用时,编译器会生成有关未使用函数的警告,但您可以通过将其更改为static inline.

Thus I tend to think that staticshould be used in translation units and benefit from extra check compiler does to find unused functions. And static inlineshould be used in header files to provide functions that can be in-lined (due to absence of external linkage) without issuing warnings.

因此我倾向于认为static应该在翻译单元中使用并从额外的检查编译器中受益,以找到未使用的函数。并且static inline应该在头文件中使用以提供可以内联(由于没有外部链接)而不发出警告的函数。

Unfortunately I cannot find any evidence for that logic. Even from GCC documentation I wasn't able to conclude that inlineinhibits unused function warnings. I'd appreciate if someone will share links to description of that.

不幸的是,我找不到任何证据证明这种逻辑。即使从 GCC 文档中,我也无法得出inline禁止未使用函数警告的结论。如果有人会分享描述的链接,我将不胜感激。

回答by shengy

In C, staticmeans the function or variable you define can be only used in this file(i.e. the compile unit)

在C中,static表示你定义的函数或变量只能在这个文件(即编译单元)中使用

So, static inlinemeans the inline function which can be used in this file only.

所以,static inline意味着只能在这个文件中使用的内联函数。

EDIT:

编辑:

The compile unitshould be The Translation Unit

编译单元应该是翻译单元

回答by R.. GitHub STOP HELPING ICE

One difference that's not at the language level but the popular implementation level: certain versions of gcc will remove unreferenced static inlinefunctions from output by default, but will keep plain staticfunctions even if unreferenced. I'm not sure which versions this applies to, but from a practical standpoint it means it may be a good idea to always use inlinefor staticfunctions in headers.

一个区别不是在语言级别而是在流行的实现级别:static inline默认情况下,某些版本的 gcc 会从输出中删除未引用的函数,但static即使未引用也会保留普通函数。我不知道这适用于哪个版本,但是从实际情况来看这意味着它可能是一个好主意,始终使用inlinestatic标头的功能。