c - 内联中file-static out-of-line、global out-of-line和no out-of-line copy函数是什么意思
问题描述
我在阅读 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;
}
因此,编译器将函数的内容嵌入到调用它的例程中。这称为内联实现。
您引用的注释使用外线将函数的正常实现称为实际子例程。这不是常用的措辞,但也并不罕见。
推荐阅读
- apache-kafka - 将消息从一个 Kafka 主题“推送”到另一个主题
- shell - 部署在 tomcat 中的应用程序无法通过 Jenkins 管道启动
- c# - WPF 文件夹浏览器对话框,显示外部设备并可以导航到用户给定的文件夹路径
- python - 无法安装pyperclip,Python的多个实例
- java - java - 如何在java中抛出带有特定错误代码的SQLException?
- arduino - 为什么我的 ESP8266 的 EEPROM 不工作?
- azure-data-explorer - 为 Kusto 导出指定多个存储帐户
- django - Django Allauth 和 Salesforce:No_Oauth_State:状态无效
- python - 使用 Python 脚本自动生成评论
- firebase - 如何从图库中设置个人资料图片并保存在 android studio 中的 Firebase