Should C declarations match definition including keywords and qualifiers such as “static”, “inline”, etc

前端 未结 4 657
心在旅途
心在旅途 2020-12-21 02:21

Consider this example of a function declaration and definition (in the same translation unit):

inline static int foo(int x);

...

int foo(int x)
{
  return          


        
4条回答
  •  半阙折子戏
    2020-12-21 02:50

    No, for inline in particular, these should not be the same.

    But the example is wrong from the start. For inline you need a definition (the whole function) with the inline in the .h file. By that, the inline, you avoid that the symbol is defined in several translation unit (.c) where you include the .h header.

    Then, in exactly one translation unit you pout just the declaration without inline to indicate that the symbol should be generated in the corresponding .o object file.

提交回复
热议问题