C语言 初始化头中的静态变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3837490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:34:05  来源:igfitidea点击:

initializing a static variable in header

cvariablesstaticlinker

提问by darven

I am new in programming in C, so I am trying many different things to try and familiarize myself with the language.

我是 C 编程的新手,所以我正在尝试许多不同的事情来尝试和熟悉该语言。

I wrote the following:

我写了以下内容:

File q7a.h:

文件q7a.h

static int err_code = 3;
void printErrCode(void);

File q7a.c:

文件q7a.c

#include <stdio.h>
#include "q7a.h"

void printErrCode(void)
{
        printf ("%d\n", err_code);
}

File q7main.c:

文件q7main.c

#include "q7a.h"

int main(void)
{
        err_code = 5;
        printErrCode();

        return 0;
}

I then ran the following in the makefile (I am using a Linux OS)

然后我在 makefile 中运行以下内容(我使用的是 Linux 操作系统)

gcc –Wall –c q7a.c –o q7a.o
gcc –Wall –c q7main.c –o q7main.o
gcc q7main.o q7a.o –o q7

the output is 3.

输出为 3。

Why is this happening?

为什么会这样?

If you initialize a static variable (in fact any variable) in the header file, so if 2 files include the same header file (in this case q7.c and q7main.c) the linker is meant to give an error for defining twice the same var?

如果您在头文件中初始化一个静态变量(实际上是任何变量),那么如果 2 个文件包含相同的头文件(在本例中为 q7.c 和 q7main.c),则链接器将给出错误定义两次相同的变量?

And why isn't the value 5 inserted into the static var (after all it is static and global)?

为什么不将值 5 插入到静态变量中(毕竟它是静态和全局的)?

Thanks for the help.

谢谢您的帮助。

回答by EboMike

staticmeans that the variable is only used within your compilation unit and will not be exposed to the linker, so if you have a static intin a header file and include it from two separate .c files, you will have two discrete copies of that int, which is most likely not at all what you want.

static意味着该变量仅在您的编译单元中使用并且不会暴露给链接器,因此如果您static int在头文件中有 a并将其包含在两个单独的 .c 文件中,您将拥有该 int 的两个离散副本,其中很可能根本不是你想要的。

Instead, you might consider extern int, and choose one .c file that actually defines it (i.e. just int err_code=3).

相反,您可以考虑extern int,并选择一个实际定义它的 .c 文件(即仅int err_code=3)。

回答by bvprakash

When you declared a variable as a static it has only scope within a file ie.., it can be accessed only within a file. When you declare a static variable in a header file and include this header file in two .c file, then you are creating two different memory for two different ".c" files

当您将变量声明为静态变量时,它仅在文件内具有作用域,即..,它只能在文件内访问。当您在头文件中声明一个静态变量并将此头文件包含在两个 .c 文件中时,您将为两个不同的“.c”文件创建两个不同的内存

When you print err_code directly in the Main function you would see its value as 5 instead 3, but you are calling a function printErrCode which is defined in a different file "q7a.c" for which err_code has a different memory location in which err_code memory is still 3 and it not updated and that is why you are getting the value as 3 instead of 5.

当您直接在 Main 函数中打印 err_code 时,您会看到它的值为 5 而不是 3,但是您正在调用在不同文件“q7a.c”中定义的函数 printErrCode,其中 err_code 具有不同的内存位置,其中 err_code 内存仍然是 3 并且它没有更新,这就是为什么你得到的值为 3 而不是 5。

Since two memory is created and err_code is considered as two different variables having different memory with different file scope you will not see any linking errors.

由于创建了两个内存并且 err_code 被视为具有不同内存和不同文件范围的两个不同变量,因此您不会看到任何链接错误。

回答by Amit

The static variables donot have external linkage which means they cannot be accessed outside the translation unit in which they are being defined. So in your case when q7.h is #include'ed in both translations units q7a.c and q7main.c ... two different copies exists in their corresponding .o files. That is why linker does not report error becuase both copies are not seen by linker while doing external symbol linkage.

静态变量没有外部链接,这意味着不能在定义它们的翻译单元之外访问它们。因此,在您的情况下,当 q7.h 在两个翻译单元 q7a.c 和 q7main.c 中都被 #include 时......它们对应的 .o 文件中存在两个不同的副本。这就是链接器不报告错误的原因,因为在执行外部符号链接时,链接器看不到两个副本。

回答by Krishna Chaitanya K

While doing small research came to know that we can declare variable in Header file but in one of the source file includes that should have definition for that variable.

在做一些小的研究时才知道我们可以在头文件中声明变量,但在源文件中的一个包含应该有该变量定义的。

Instead if we define a variable in header file. in the source files where this header file included, definitions will be created which causes multiple definitions.

相反,如果我们在头文件中定义一个变量。在包含此头文件的源文件中,将创建定义,从而导致多个定义。

A static variable should be declared with in the file where we use it shouldn't be exposed to header file.

静态变量应该在我们使用它的文件中声明,它不应该暴露给头文件。

Hope I am giving right information. If I am wrong feel free to correct my statements in your comments.

希望我提供正确的信息。如果我错了,请随时纠正我在评论中的陈述。