为什么函数定义在C中隐式地在外部? [英] Why are function definitions implicitly external in C?

查看:58
本文介绍了为什么函数定义在C中隐式地在外部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到 extern 关键字在函数上下文中是隐式的,因此,除非您另外指定使用 static 关键字(错误基本上是与变量所使用的 static (它们只是共享一个关键字)完全不同的概念,它们对所有目标文件都是可见的.这很有道理;将声明隐式地放在外部,而当声明与定义在同一文件中时,从技术上讲是不必要的,这很有用,因为程序员不必每次想使用函数时都键入 extern 它的定义文件,通常是这样.在我看来,奇怪的是声明和定义都是隐式的.

I read that the extern keyword is implicit in the context of functions, so unless you specify otherwise using the static keyword (which if I'm not mistaken is basically a completely separate concept from the static that variables employ—they just share a keyword), they are visible to all object files. This makes sense; having the declarations be implicitly external, while technically unnecessary when the declarations are in the same file as the definition, is useful because the programmer doesn't have to type extern every time they want to use a function out of its defining file, which is more often the case than not. What seems odd to me is that it is implicit for the declarations and the definition.

使用变量,不需要为定义添加 extern ,实际上,尽管我可以做到这一点而没有错误,但编译器会为此警告我.

With a variable, I don't need to include an extern for the definition, and in fact, while I can do this without error, my compiler gives me a warning for it.

例如,我可以拥有 mylib.h :

int var = 5;

//it isn't necessary to write this as
//extern int var = 5;
//my compiler even warns against it

test.c

#include "mylib.h"

extern int var;

我通常会假定函数的隐式 extern 是相同的,也就是说,如果我在 mylib.h中定义了 int func(int par),在它之前没有隐式的 extern ,但是对于它的任何声明都会有一个隐式的 extern (例如,如果我声明要使用)在 test.c 中).

I would normally assume the implicit extern for functions to be the same, that is, if I defined int func(int par) in mylib.h, there would not be an implicit extern before it, but there would be an implicit extern for any declaration of it (such as if I declared it for use in test.c).

extern 关键字的角度来看,这也没有多大意义,因为该关键字告诉编译器在其他地方查找该定义,而该定义永远不会超出该定义的范围.档案在其中.

It also doesn't make much sense from the perspective of the extern keyword being used as a way of telling the compiler to look elsewhere for the definition, when the definition would never be external of the file it is in.

我觉得我在这里想念什么.

I feel like I'm missing something here.

推荐答案

如果在任何头文件中使用 int x = 10; ,则可能会遇到麻烦,因为如果包含相同的内容与 test.c 链接的任何其他文件( .c .h )中的头文件,则会出现错误重新定义变量x .

If you use int x = 10; in any header file, then you are entering into trouble, because if you include the same header file in any other file (.c or .h) that is linked with test.c then you will get an error redefinition of variable x.

您可以自己尝试.

因此,始终将 extern int x; 保留在头文件中,并在任何 .c 文件中将其定义为 int x = 10; .

So always keep extern int x; in a header file, and define it int x = 10; in any .c file.

因此,在这种情况下,如果将头文件包含在多个位置,那很好,因为头文件仅具有一个声明,并且可以在多个位置声明相同的变量而没有任何问题.

So, in this case, if you include the header file in multiple places, it is fine, because the header file only has a declaration and you can declare the same variable in multiple places without any problem.

您可以尝试使用此示例程序来测试错误 "global_value"的多个定义

you can try this sample program to test the error multiple definition of `global_value'

test.h

extern int global_value;

test.c

#include <stdio.h>
#include "test.h"
int global_value = 10;

int test_func()
{
        printf("golbal_value = %d", global_value);
        global_value = 20; // changed here, reflect in main after test_func call
}

main.c

#include <stdio.h>
#include "test.h"
int main()
{
        test_func();
        printf("global_value = %d\n", global_value);
        return 0;
}

以上程序运行完美.要获取错误,请将 extern int global_value; 放入 test.c int global_value = 10; 放入 test.h 并一起编译 gcc test.c main.c

the above program works perfectly. to get the error bring that extern int global_value; to test.c and int global_value = 10; to test.h and compile all together gcc test.c main.c

这篇关于为什么函数定义在C中隐式地在外部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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