在C99内联说明符标准中(6.7.4第6段)规定:
"如果翻译中函数的所有文件范围声明 单元包括inline功能说明符而不包括extern, 那么该翻译单元中的定义是inline definition.An inline definition does not provide an external definition for the function,并且不禁止外部 另一个翻译单元中的定义"
然而,当我用godbolt编译以下代码时:
static inline int foo(int a) {
a = 1;
return a; }
int main(void){
return foo(-1);
}
,结果包括外部定义(应该具有内部链接):
foo: ; an external definition with internal linkage
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-4], edi
mov DWORD PTR [rbp-4], 1
mov eax, DWORD PTR [rbp-4]
pop rbp
ret
main:
push rbp
mov rbp, rsp
mov edi, -1
call foo
pop rbp
ret
I think the definition of function foo()
in source code is a inline definition and thus the assembly result should not contain the definition just like when we declare it with raw inline, but it seems not, why?
Note that, if we compile the code following in godbolt:
inline int foo(int a) {
a = 1;
return a; }
int main(void){
return foo(-1);
}
结果将是:
; Here foo() is omitted because inline definition
main:
push rbp
mov rbp, rsp
mov edi, -1
call foo
pop rbp
ret
当foo的定义都是内联定义时,为什么foo确实出现在前一个中,而不是在后一个中?
我在谷歌上搜索了很多,有很多关于静态内联的QA,但没有一个提到上面的行为.