头文件中的静态const变量声明

时间:2022-08-08 15:07:20

If I declare static const variable in header file like this:

如果我在头文件中声明静态const变量,如下所示:

static const int my_variable = 1;

and then include this header in more than one .c files, will compilator make new instance per each file or will be "smart" enough to see it is const and will make only one instance for all the files?

然后将这个标题包含在多个.c文件中,编译器会为每个文件创建新实例,还是“智能”,足以看到它是const并且只为所有文件创建一个实例?

I know I can make it extern and define it in one of .c files that include this header but this is what I am trying not to do.

我知道我可以将它设置为extern并在包含此标题的.c文件中定义它,但这是我不想做的。

3 个解决方案

#1


10  

I answered this at length here. That answer is for C++, but it holds true for C as well.

我在这里详细回答了这个问题。这个答案适用于C ++,但它也适用于C语言。

The translation unit is the individual source file. Each translation unit including your header will "see" a static const int. The static, in this context, means the scope of my_variable is limited to the translation unit. So you end up with a separate my_variable for each translation unit (".c file").

翻译单元是单独的源文件。包含标题的每个翻译单元将“看到”一个静态const int。在此上下文中,静态意味着my_variable的范围仅限于翻译单元。因此,您最终会为每个翻译单元(“.c文件”)添加一个单独的my_variable。

The compiler would not be "smart" to create only one instance for all files, it would be faulty, because you explicitly told it not to do so (static).

编译器不会“智能”为所有文件只创建一个实例,它会有问题,因为你明确告诉它不要这样做(静态)。

#2


3  

If you use address of that object - compiler surely creates one instance per each translation unit. If you use only value - it is probably smart enough to avoid creating of object at all - value will be inlined where need.

如果您使用该对象的地址 - 编译器肯定会为每个翻译单元创建一个实例。如果你只使用值 - 它可能足够聪明,可以避免创建对象 - 值将在需要的地方内联。

#3


-1  

I guess it will make only one instance for all files. But you can verify it by calling it in different files and check its value

我想它只会为所有文件生成一个实例。但您可以通过在不同的文件中调用它并检查其值来验证它

#1


10  

I answered this at length here. That answer is for C++, but it holds true for C as well.

我在这里详细回答了这个问题。这个答案适用于C ++,但它也适用于C语言。

The translation unit is the individual source file. Each translation unit including your header will "see" a static const int. The static, in this context, means the scope of my_variable is limited to the translation unit. So you end up with a separate my_variable for each translation unit (".c file").

翻译单元是单独的源文件。包含标题的每个翻译单元将“看到”一个静态const int。在此上下文中,静态意味着my_variable的范围仅限于翻译单元。因此,您最终会为每个翻译单元(“.c文件”)添加一个单独的my_variable。

The compiler would not be "smart" to create only one instance for all files, it would be faulty, because you explicitly told it not to do so (static).

编译器不会“智能”为所有文件只创建一个实例,它会有问题,因为你明确告诉它不要这样做(静态)。

#2


3  

If you use address of that object - compiler surely creates one instance per each translation unit. If you use only value - it is probably smart enough to avoid creating of object at all - value will be inlined where need.

如果您使用该对象的地址 - 编译器肯定会为每个翻译单元创建一个实例。如果你只使用值 - 它可能足够聪明,可以避免创建对象 - 值将在需要的地方内联。

#3


-1  

I guess it will make only one instance for all files. But you can verify it by calling it in different files and check its value

我想它只会为所有文件生成一个实例。但您可以通过在不同的文件中调用它并检查其值来验证它