C声明是否应与包括关键字和限定符(例如"static","inline"等)的定义匹配 [英] Should C declarations match definition including keywords and qualifiers such as "static", "inline", etc

查看:53
本文介绍了C声明是否应与包括关键字和限定符(例如"static","inline"等)的定义匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑此函数声明和定义的示例(在同一翻译单元中):

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 x+1;
}

我知道类型需要匹配,但是其他关键字和限定词呢?两种情况下都应该inline static吗?还是只是声明?

I know that the types need to match, but what about other keywords and qualifiers? Should inline static be in both cases? Or just the declaration?

我可以使用C标准的哪一部分或编码准则来证明答案的正确性?

And which part of the C standard or which coding guideline could I use to justify the answer?

推荐答案

不,特别是对于inline,这些不应相同.

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

但是这个例子从一开始就是错误的.对于inline,您需要一个.h文件中带有inline的定义(整个功能).这样,inline避免了在包含.h标头的几个翻译单元(.c)中定义符号的情况.

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.

然后,在一个翻译单元中,仅输出不带inline的声明,以指示该符号应在相应的.o目标文件中生成.

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.

这篇关于C声明是否应与包括关键字和限定符(例如"static","inline"等)的定义匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆