如果函数来"&的extern QUOT;在头文件? [英] Should functions be made "extern" in header files?

查看:149
本文介绍了如果函数来"&的extern QUOT;在头文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果函数来的extern 在头文件?或者它们的extern 默认?

例如,我应该写这样的:

  // birthdays.h
结构的人find_birthday(为const char *名称);

或本

  // birthdays.h
结构的extern人find_birthday(为const char *名称);


解决方案

的C书:


  

如果一个声明中包含将extern
  存储类说明,或者是
  与函数声明没有
  存储类说明(或两者)
  那么:


  
  

      
  • 如果已存在该标识符与一个可见的声明
      文件范围,由此产生的联动性
      相同的可见的
      声明;

  •   
  • 否则结果为外部连接。

  •   

因此​​,如果这是它在翻译单元中声明唯一的一次,就会产生外部链接。

Should functions be made extern in header files? Or are they extern by default?

For example, should I write this:

// birthdays.h
struct person find_birthday(const char* name);

or this:

// birthdays.h
extern struct person find_birthday(const char* name);

解决方案

From The C Book:

If a declaration contains the extern storage class specifier, or is the declaration of a function with no storage class specifier (or both), then:

  • If there is already a visible declaration of that identifier with file scope, the resulting linkage is the same as that of the visible declaration;
  • otherwise the result is external linkage.

So if this is the only time it's declared in the translation unit, it will have external linkage.

这篇关于如果函数来"&的extern QUOT;在头文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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