在 C99中,没有“ static”或“ extern”的“ inline”是否有用?

当我尝试构建这个代码时

inline void f() {}


int main()
{
f();
}

使用命令行

gcc -std=c99 -o a a.c

我得到一个链接器错误(对 f的未定义引用)。如果使用 static inlineextern inline而不仅仅是 inline,或者使用 -O编译(因此函数实际上是内联的) ,则错误消失。

这种行为似乎在 C99标准的第6.7.4(6)段中得到了定义:

如果一个翻译单元中的函数的所有文件范围声明都包含没有 externinline函数说明符,那么该翻译单元中的定义就是一个内联定义。内联定义不为函数提供外部定义,也不禁止在另一个翻译单元中使用外部定义。内联定义提供了外部定义的替代方案,翻译人员可以使用外部定义实现对同一翻译单元中的函数的任何调用。未指定对函数的调用是使用内联定义还是使用外部定义。

如果我的理解正确的话,一个编译单元的函数定义为 inline,就像上面的例子一样,只有当一个外部函数的名字相同的时候,编译单元才能一致地进行编译,而且我从来不知道我自己的函数或者外部函数是否被调用。

这种行为不是很愚蠢吗?在 C99中定义一个没有 staticextern的函数 inline有用吗?我错过了什么吗?

答案摘要

当然,我遗漏了一些东西,这种行为并不愚蠢。 :)

作为 尼莫解释道,其思想是把函数的 定义

inline void f() {}

在头文件中,只有一个 声明

extern inline void f();

在相应的。文件。只有 extern声明触发生成外部可见的二进制代码。而且 inline在。C 文件——它只在头文件中有用。

正如 乔纳森在回答中引用了 C99委员会的理由解释的那样,inline完全是关于编译器优化的,它要求函数的定义在调用的站点上可见。这只能通过将定义放在标头中来实现,当然,标头中的定义不能每次被编译器看到时都发出代码。但是,由于编译器不必实际内联函数,因此外部定义必须存在于某个地方。

39243 次浏览

Actually this excellent answer also answers your question, I think:

What does extern inline do?

The idea is that "inline" can be used in a header file, and then "extern inline" in a .c file. "extern inline" is just how you instruct the compiler which object file should contain the (externally visible) generated code.

[update, to elaborate]

I do not think there is any use for "inline" (without "static" or "extern") in a .c file. But in a header file it makes sense, and it requires a corresponding "extern inline" declaration in some .c file to actually generate the stand-alone code.

> I get a linker error (undefined reference to f)

Works here: Linux x86-64, GCC 4.1.2. May be a bug in your compiler; I don't see anything in the cited paragraph from the standard that forbids the given program. Note the use of if rather than iff.

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.

So, if you know the behavior of the function f and you want to call it in a tight loop, you may copy-paste its definition into a module to prevent function calls; or, you may provide a definition that, for the purposes of the current module, is equivalent (but skips input validation, or whatever optimization you can imagine). The compiler writer, however, has the option of optimizing for program size instead.

From the standard (ISO/IEC 9899:1999) itself:

Appendix J.2 Undefined Behaviour

  • ...
  • A function with external linkage is declared with an inline function specifier, but is not also defined in the same translation unit (6.7.4).
  • ...

The C99 Committee wrote a Rationale, and it says:

6.7.4 Function specifiers

A new feature of C99: The inline keyword, adapted from C++, is a function-specifier that can be used only in function declarations. It is useful for program optimizations that require the definition of a function to be visible at the site of a call. (Note that the Standard does not attempt to specify the nature of these optimizations.)

Visibility is assured if the function has internal linkage, or if it has external linkage and the call is in the same translation unit as the external definition. In these cases, the presence of the inline keyword in a declaration or definition of the function has no effect beyond indicating a preference that calls of that function should be optimized in preference to calls of other functions declared without the inline keyword.

Visibility is a problem for a call of a function with external linkage where the call is in a different translation unit from the function’s definition. In this case, the inline keyword allows the translation unit containing the call to also contain a local, or inline, definition of the function.

A program can contain a translation unit with an external definition, a translation unit with an inline definition, and a translation unit with a declaration but no definition for a function. Calls in the latter translation unit will use the external definition as usual.

An inline definition of a function is considered to be a different definition than the external definition. If a call to some function func with external linkage occurs where an inline definition is visible, the behavior is the same as if the call were made to another function, say __func, with internal linkage. A conforming program must not depend on which function is called. This is the inline model in the Standard.

A conforming program must not rely on the implementation using the inline definition, nor may it rely on the implementation using the external definition. The address of a function is always the address corresponding to the external definition, but when this address is used to call the function, the inline definition might be used. Therefore, the following example might not behave as expected.

inline const char *saddr(void)
{
static const char name[] = "saddr";
return name;
}
int compare_name(void)
{
return saddr() == saddr(); // unspecified behavior
}

Since the implementation might use the inline definition for one of the calls to saddr and use the external definition for the other, the equality operation is not guaranteed to evaluate to 1 (true). This shows that static objects defined within the inline definition are distinct from their corresponding object in the external definition. This motivated the constraint against even defining a non-const object of this type.

Inlining was added to the Standard in such a way that it can be implemented with existing linker technology, and a subset of C99 inlining is compatible with C++. This was achieved by requiring that exactly one translation unit containing the definition of an inline function be specified as the one that provides the external definition for the function. Because that specification consists simply of a declaration that either lacks the inline keyword, or contains both inline and extern, it will also be accepted by a C++ translator.

Inlining in C99 does extend the C++ specification in two ways. First, if a function is declared inline in one translation unit, it need not be declared inline in every other translation unit. This allows, for example, a library function that is to be inlined within the library but available only through an external definition elsewhere. The alternative of using a wrapper function for the external function requires an additional name; and it may also adversely impact performance if a translator does not actually do inline substitution.

Second, the requirement that all definitions of an inline function be “exactly the same” is replaced by the requirement that the behavior of the program should not depend on whether a call is implemented with a visible inline definition, or the external definition, of a function. This allows an inline definition to be specialized for its use within a particular translation unit. For example, the external definition of a library function might include some argument validation that is not needed for calls made from other functions in the same library. These extensions do offer some advantages; and programmers who are concerned about compatibility can simply abide by the stricter C++ rules.

Note that it is not appropriate for implementations to provide inline definitions of standard library functions in the standard headers because this can break some legacy code that redeclares standard library functions after including their headers. The inline keyword is intended only to provide users with a portable way to suggest inlining of functions. Because the standard headers need not be portable, implementations have other options along the lines of:

#define abs(x) __builtin_abs(x)

or other non-portable mechanisms for inlining standard library functions.