c - 为什么普通 header 中的内联 void foo() { ... } 可以正常工作?

标签 c static inline

考虑在 C99 中使用 inline,以及各种选项(extern'ing、static inline 等),如 here 所述,例如。

我不明白为什么 C 标准不允许只使用

void inline foo() { do_stuff(); }

在一个通用的头文件中,并且可以在任何地方使用。为什么我必须添加 static?我想要的还不够清楚吗?

最佳答案

来自gcc site :

When an inline function is not static, then the compiler must assume that there may be calls from other source files; since a global symbol can be defined only once in any program, the function must not be defined in the other source files, so the calls therein cannot be integrated. Therefore, a non-static inline function is always compiled on its own in the usual fashion.

这对你意味着什么。您不能在 header 中定义内联函数,因为这意味着它将从不同的翻译单元(包含它的 .c 文件)定义。 一旦定义了 static inline,那么包含 header 的每个 c 文件都将拥有自己的函数副本。

编辑

gcc 行为正确。随着 C99 的引入,内联函数被添加到标准中。该标准有些含糊不清,并指出:

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.

它的实际意思是,当编译器看到 inline 关键字时,无法知道该函数是否已经在另一个翻译单元中定义(这只有在链接期间才能知道)。现在大多数包含 gcc 的编译器都不会在没有打开优化的情况下内联函数。因此,如果您在使用 -O0 进行编译时尝试使用内联函数,编译器会发出对该函数的真正调用(它假定它是在另一个文件中定义的,然后是当前正在编译的文件)。现在,当链接器遇到调用时,它会尝试在所有已编译对象中查找该函数但失败了(因为编译器没有为该函数创建主体,也没有内联它——标准说它不必创建主体对于函数,它可以假设在某个地方有一个定义)。这就是您在链接项目时收到错误的原因:undefined reference to f。这是使用 -std=c99 进行编译时的正确行为。这也意味着,如果您希望内联函数具有主体,则必须仅将其声明为外部一次但是您还必须在使用外部链接声明函数的同一翻译单元内提供定义。

因此,为了让您的代码根据标准正确工作,您应该执行以下操作:

在 h 文件中按照您期望的方式执行即可。

inline void f(){ /*DO SOMETHING*/}

在您的一个代码文件(.c 文件)中,您可以:

extern inline void f();

现在发生的事情是,编译器现在只在一个翻译单元中遇到了主体定义(取自 header )和声明它应该存在于某处的外部定义,并且根据标准为函数创建了一个。

因此标准规定任何具有内部链接的函数都可以声明为内联,并且编译器应该为它生成一个函数体。在 C 中,只有在添加 static 关键字时,函数才被认为具有内部链接。 这就是 static inline void f() 起作用的原因。如果没有 static 关键字,编译器会假定该函数具有外部链接,这就是为什么只指定 inline void f() 而没有在其中添加 extern inline void f() 的原因源文件将不起作用。

关于c - 为什么普通 header 中的内联 void foo() { ... } 可以正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33301404/

相关文章:

c - 如何使用 CreateThread() 创建多个线程,每个线程具有不同的 ThreadProc() 函数

css - django 样式表不与图像文件链接

C++ 内联函数 : declare as such, 是这样定义的,还是两者都定义?为什么?

html - 显示 :inline isn't making elements appear on one row

inline - SWIG : What is the different between "%inline %{ %}" and "%{ %}"?

你能解释一下 *ptr++ 和 *(++ptr) 之间的区别吗?

c - C有类吗?

python - 在 Linux 上伪造 IO 错误

java - Java中隐藏的静态方法

c - 为什么要在main中声明一个静态变量?