gpt4 book ai didi

c - 内联中的文件静态外线、全局外线和无外线函数副本是什么意思

转载 作者:行者123 更新时间:2023-11-30 14:36:23 25 4
gpt4 key购买 nike

我正在阅读OS/161的源代码并遇到内联支持代码。我无法理解所提供的评论。评论是:-

/*                                                                                                                                                                                                          
* Material for supporting inline functions.
*
* A function marked inline can be handled by the compiler in three
* ways: in addition to possibly inlining into the code for other
* functions, the compiler can (1) generate a file-static out-of-line
* copy of the function, (2) generate a global out-of-line copy of the
* function, or (3) generate no out-of-line copy of the function.
*
* None of these alone is thoroughly satisfactory. Since an inline
* function may or may not be inlined at the compiler's discretion, if
* no out-of-line copy exists the build may fail at link time with
* undefined symbols. Meanwhile, if the compiler is told to generate a
* global out-of-line copy, it will generate one such copy for every
* source file where the inline definition is visible; since inline
* functions tend to appear in header files, this leads to multiply
* defined symbols and build failure. The file-static option isn't
* really an improvement, either: one tends to get compiler warnings
* about inline functions that haven't been used, which for any
* particular source file tends to be at least some of the ones that
* have been defined. Furthermore, this method leads to one
* out-of-line copy of the inline function per source file that uses
* it, which not only wastes space but makes debugging painful.
*/

请谁能解释一下文件静态外联、全局外联和函数没有外联副本是什么意思

最佳答案

函数的“正常”实现将其创建为编译器生成的汇编语言或机器代码中的子例程:它是由其他例程调用的指令序列,并在完成时返回。

由于调用函数并从函数返回可能会产生一些开销,因此编译器可能会生成该函数的内联实现。例如,使用以下代码:

int square(int x) { return x*x; }
...
void foo(...)
{
...
y = square(x);
}

编译器可以选择编译foo,就好像它是:

void foo(...)
{
...
y = x*x;
}

因此,编译器将函数的内容嵌入到调用它的例程中。这称为内联实现。

您引用的注释使用out-of-line来指代函数作为实际子例程的正常实现。这不是常用的措辞,但也并不罕见。

关于c - 内联中的文件静态外线、全局外线和无外线函数副本是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58099159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com