一个定义规则-内联函数的多个定义 [英] One Definition Rule - Multiple definition of inline functions
问题描述
我正在阅读ODR,并且按规则说在整个程序中,对象或非内联函数不能具有多个定义
,我尝试了以下操作...
I was reading ODR and as the rule says "In the entire program, an object or non-inline function cannot have more than one definition"
and I tried the following...
file1.cpp
file1.cpp
#include <iostream>
using namespace std;
inline int func1(void){ return 5; }
inline int func2(void){ return 6; }
inline int func3(void){ return 7; }
int sum(void);
int main(int argc, char *argv[])
{
cout << func1() << endl;
cout << func2() << endl;
cout << func3() << endl;
cout << sum() << endl;
return 0;
}
file2.cpp
file2.cpp
inline int func1(void) { return 5; }
inline int func2(void) { return 6; }
inline int func3(void) { return 7; }
int sum(void) { return func1() + func2() + func3(); }
它按规则说是有效的。我可以有多个内联函数定义。
It worked as the rule says. I can have multiple definition of inline functions.
- 非内联函数链接和内联函数链接有什么区别?
- 链接程序可区分这两个?
推荐答案
使函数 inline
做两件事(第二点与您的问题更相关):
Making a function inline
does two things (the second point is more relevant to your question):
-
这是一个建议程序员对编译器的调用,可以通过内联扩展来快速调用此函数。粗略地说,内联扩展类似于将内联函数视为宏,通过其主体代码扩展对其的每次调用。这是一个建议-编译器可能不会(有时不能)执行各种优化。
It is a suggestion by the programmer to the compiler, to make calls to this function fast, possibly by doing inline expansion. Roughly, inline expansion is similar to treating the inline function like a macro, expanding each call to it, by the code of its body. This is a suggestion - the compiler may not (and sometimes cannot) perform various optimizations like that.
它指定了该功能是翻译单元的功能。因此,如果 inline
函数出现在 foo.cpp
中(要么是因为它被写在其中,要么是因为它是 #include
是在其中写入的标头,在这种情况下,预处理器基本上是这样做的)。现在,您编译 foo.cpp
,可能还编译其他的 bar.cpp
,其中也包含一个 inline
函数(可能是完全相同的签名;可能是由于 #include
使用相同的标头)。当链接器链接两个目标文件时,它不会被视为违反ODR,因为 inline
指令使文件的每个副本都位于其翻译单元的本地(通过编译有效创建的目标文件)。这不是建议,而是绑定。
It specifies the scope of the function to be that of a translation unit. So, if an inline
function appears in foo.cpp
(either because it was written in it, or because it #include
s a header in which it was written, in which case the preprocessor basically makes it so). Now you compile foo.cpp
, and possibly also some other bar.cpp
which also contains an inline
function with the same signature (possibly the exact same one; probably due to both #include
ing the same header). When the linker links the two object files, it will not be considered a violation of the ODR, as the inline
directive made each copy of the file local to its translation unit (the object file created by compiling it, effectively). This is not a suggestion, it is binding.
这些并非巧合两件事在一起。最常见的情况是 inline
函数出现在多个源文件的头文件 #include
d中程序员希望请求快速的内联扩展。不过,这需要翻译单元的本地性规则,这样才不会出现链接器错误。
It is not coincidental that these two things go together. The most common case is for an inline
function to appear in a header #include
d by several source files, probably because the programmer wanted to request fast inline expansion. This requires the translation-unit locality rule, though, so that linker errors shouldn't arise.
这篇关于一个定义规则-内联函数的多个定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!