cgccinlinec99

Why "static" specifier generates an external definition for inline function?


In C99 standard for inline specifier(6.7.4 paragraph 6) it states:

"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"

However, when I compile the following code in godbolt:

static inline int foo(int a) {
    a = 1;
    return a; }


int main(void){
    return foo(-1);
}

, the result includes an external definition(should be with internal linkage) :

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);
}

The result will be:

; Here foo() is omitted because inline definition
main:
        push    rbp
        mov     rbp, rsp
        mov     edi, -1
        call    foo
        pop     rbp
        ret

Why foo does appear in former one but not in latter one when definitions of foo are all the inline definition?

I have searched a lot in google and there are many QA about static inline, but none of them refers the behaviour above.


Solution

  • The sentences you quote appear after the introduction “For a function with external linkage, the following restrictions apply:”. Although the sentences you quote are not stating a restriction, they are setting up terminology definitions for the restrictions. I would interpret them to be solely in the context of a function with external linkage. They do not apply to functions with internal linkage.