在C函数extern关键字的影响。 [英] Effects of the extern keyword on C functions

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

问题描述

在C,我没有注意到之前的extern函数声明中使用关键字的任何影响。
起初,我以为定义时,的extern INT F(); 在一个文件中的力量你实现它文件的范围之外。不过,我发现,这两个:

In C, I did not notice any effect of the extern keyword used before function declaration. At first, I thought that when defining extern int f(); in a single file forces you to implement it outside of the file's scope. However I found out that both:

extern int f();
int f() {return 0;}

extern int f() {return 0;}

编译就好了,没有来自海湾合作委员会的警告。我用的gcc -Wall -ansi ;它甚至不会接受 // 的意见。

compile just fine, with no warnings from gcc. I used gcc -Wall -ansi; it wouldn't even accept // comments.

有没有使用任何特效的extern 前函数定义?或者是它只是一个可选关键字,没有副作用的功能。

Are there any effects for using extern before function definitions? Or is it just an optional keyword with no side effects for functions.

在后一种情况下,我不明白为什么做了标准的设计师选择了垃圾语法与多余的关键字。

In the latter case I don't understand why did the standard designers chose to litter the grammar with superfluous keywords.

编辑::要澄清一下,我知道有使用了的extern 中的变量,但我只问 EXTERN 中的功能

To clarify, I know there's usage for extern in variables, but I'm only asking about extern in functions.

推荐答案

我们有两个文件,​​文件foo.c和bar.c。

We have two files, foo.c and bar.c.

下面是foo.c的

#include <stdio.h>

volatile unsigned int stop_now = 0;
extern void bar_function(void);

int main(void)
{
  while (1) {
     bar_function();
     stop_now = 1;
  }
  return 0;
}

现在,这里是bar.c

Now, here is bar.c

#include <stdio.h>

extern volatile unsigned int stop_now;

void bar_function(void)
{
   while (! stop_now) {
      printf("Hello, world!\n");
      sleep(30);
   }
}

正如你所看到的,我们的foo.c和bar.c之间没有共享的头,但bar.c需要时,它的链接文件foo.c中宣布的东西,并从的foo.c需要bar.c功能时,它的链接。

As you can see, we have no shared header between foo.c and bar.c , however bar.c needs something declared in foo.c when it's linked, and foo.c needs a function from bar.c when it's linked.

通过使用'EXTERN',你告诉,无论它后面会在链接时发现的(非静态)编译器,不为她预留任何事情,因为这将在后面遇到。

By using 'extern', you are telling the compiler that whatever follows it will be found (non-static) at link time, don't reserve anything for it since it will be encountered later.

如果您需要共享的模块之间的一些全球性,不希望把/在头初始化这是非常有用的。

It's very useful if you need to share some global between modules and don't want to put / initialize it in a header.

从技术上讲,在库公共头每一个功能是的extern',但是标记它们的方式有很几乎没有益处,这取决于编译器。大多数编译器可以明白这一点自己。如你所见,这些函数实际上是定义在别处。

Technically, every function in a library public header is 'extern', however labeling them as such has very little to no benefit, depending on the compiler. Most compilers can figure that out on their own. As you see, those functions are actually defined somewhere else.

在上面的例子中,main()的将打印的hello world只有一次,但继续进入bar_function()。还要注意,bar_function()是不会在这个例子中,返回(因为它只是一个简单的例子)。试想一下,当如果这似乎并不够实用的信号被服务stop_now被修改。

In the above example, main() would print hello world only once, but continue to enter bar_function(). Also note, bar_function() is not going to return in this example (since it's just a simple example). Just imagine stop_now being modified when a signal is serviced if this doesn't seem practical enough.

实习医生都喜欢的事情信号处理程序,您不希望把页眉或结构等互斥大多数编译器将优化,以确保它们不会保留对外部对象的任何记忆非常有用的,因为他们知道他们会在哪里定义对象的模块中保留它。但是,同样,几乎没有点原型公共职能时现代编译器指定它。

Externs are very useful for things like signal handlers, a mutex that you don't want to put in a header or structure, etc. Most compilers will optimize to ensure that they don't reserve any memory for external objects, since they know they'll be reserving it in the module where the object is defined. However, again, there's little point in specifying it with modern compilers when prototyping public functions.

希望有所帮助:)

这篇关于在C函数extern关键字的影响。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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