为什么EXTERN应用于定义? [英] Why can extern be applied to definitions?

查看:101
本文介绍了为什么EXTERN应用于定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是为什么合法的吗?

extern int foo = 0xF00; // Gets a warning, still compiles

extern void bar() { // No warning
  int x;
}

有一个理由,为什么这是允许的?

Is there a reason to why this is allowed?

推荐答案

有时是有用的。

extern const int foo = 0xF00;

如果没有的extern ,在C ++ 静态和有内部链接(这意味着你不能从另一个翻译单位使用)。

Without the extern, in C++ foo would be static and have internal linkage (which means you could not use foo from another translation unit).

的extern 在这两种情况下在你的例子是多余的。 <一href=\"http://stackoverflow.com/questions/2217628/multiple-definition-of-inline-functions-when-linking-static-libs/2218034#2218034\">In C99的的extern 可以为在线函数的区别。。

The extern in both cases in your example is redundant. In C99 an extern can make a difference for inline functions..

这篇关于为什么EXTERN应用于定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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