Is “inline” without “static” or “extern” ever useful in C99?

前端 未结 3 992
清歌不尽
清歌不尽 2020-11-22 04:35

When I try to build this code

inline void f() {}

int main()
{
    f();
}

using the command line

gcc -std=c99 -o a a.c
         


        
3条回答
  •  伪装坚强ぢ
    2020-11-22 05:31

    > I get a linker error (undefined reference to f)

    Works here: Linux x86-64, GCC 4.1.2. May be a bug in your compiler; I don't see anything in the cited paragraph from the standard that forbids the given program. Note the use of if rather than iff.

    An inline definition provides an alternative to an external definition, which a translator may use to implement any call to the function in the same translation unit.

    So, if you know the behavior of the function f and you want to call it in a tight loop, you may copy-paste its definition into a module to prevent function calls; or, you may provide a definition that, for the purposes of the current module, is equivalent (but skips input validation, or whatever optimization you can imagine). The compiler writer, however, has the option of optimizing for program size instead.

提交回复
热议问题