如何正确使用在C extern关键字 [英] How to correctly use the extern keyword in C

查看:165
本文介绍了如何正确使用在C extern关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是关于当一个函数应与的extern 关键字在C中引用。

My question is about when a function should be referenced with the extern keyword in C.

我没有看到这个的时候应该在实践中使用。由于我通过头文件写一个程序所有这一切我使用该功能的提供我已经包括在内。所以,为什么会是有用的的extern 以访问的东西,这不是在头文件中暴露的?

I am failing to see when this should be used in practice. As I am writing a program all of the functions that I use are made available through the header files I have included. So why would it be useful to extern to get access to something that was not exposed in the header file?

我就可以想怎么的extern 工作正确,如果是的话请指正。

I could be thinking about how extern works incorrectly, and if so please correct me.

编辑:您应该的extern 东西的时候它没有在头文件中的关键字默认声明

Should you extern something when it is the default declaration without the keyword in a header file?

推荐答案

外部变化的联系。与关键字,函数/变量被假定为可用的其他地方,并分辨被推迟到接头

"extern" changes the linkage. With the keyword, the function / variable is assumed to be available somewhere else and the resolving is deferred to the linker.

有一个关于函数和变量外部之间的差异:对变量它不会实例变量本身,即不分配任何内存。这需要别的地方完成。因此,如果你想从别的地​​方进口变量是很重要的。对于函数,这只是告诉编译器联动是EXTERN。由于这是默认的(使用关键字静态,表示使用extern链接的功能未绑定),你不需要明确使用它。

There's a difference between "extern" on functions and on variables: on variables it doesn't instantiate the variable itself, i.e. doesn't allocate any memory. This needs to be done somewhere else. Thus it's important if you want to import the variable from somewhere else. For functions, this only tells the compiler that linkage is extern. As this is the default (you use the keyword "static" to indicate that a function is not bound using extern linkage) you don't need to use it explicitly.

这篇关于如何正确使用在C extern关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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