头文件中的变量声明

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

In case I have a variable that may be used in several sources - is it a good practice to declare it in a header? or is it better to declare it in a .c file and use extern in other files?

如果我有一个变量可以在多个源中使用——在头中声明它是一个很好的实践吗?还是在.c文件中声明它,在其他文件中使用extern ?

5 个解决方案

#1


91  

You should declare the variable in a header file:

您应该在头文件中声明变量:

extern int x;

and then define it in one C file:

然后在一个C文件中定义:

int x;

In C, the difference between a definition and a declaration is that the definition reserves space for the variable, whereas the declaration merely introduces the variable into the symbol table (and will cause the linker to go looking for it when it comes to link time).

在C语言中,定义和声明之间的区别在于定义为变量保留了空间,而声明只是将变量引入符号表(并将导致链接器在链接时查找它)。

#2


29  

You can (should) declare it as extern in a header file, and define it in exactly 1 .c file.

您可以(应该)在头文件中声明它为extern,并在1 .c文件中定义它。

Note that that .c file should also use the header and that the standard pattern looks like:

注意.c文件也应该使用header,标准模式如下:

// file.h
extern int x;  // declaration

// file.c
#include "file.h"
int x = 1;    // definition and re-declaration

#3


9  

If you declare it like

如果你像这样申报的话

int x;

in a header file which is then included in multiple places, you'll end up with multiple instances of x (and potentially compile or link problems).

在一个头文件中,这个头文件被包含在多个地方,最终您将得到x的多个实例(以及潜在的编译或链接问题)。

The correct way to approach this is to have the header file say

处理这个问题的正确方法是让头文件说。

extern int x; /* declared in foo.c */

and then in foo.c you can say

然后在foo。c你可以说

int x; /* exported in foo.h */

THen you can include your header file in as many places as you like.

然后你可以把你的头文件包含在任何你喜欢的地方。

#4


3  

The key is to keep the declarations of the variable in the header file and source file the same.

关键是保持头文件和源文件中变量的声明不变。

I use this trick

我使用这个技巧

------sample.c------
#define sample_c
#include sample.h

(rest of sample .c)

------sample.h------
#ifdef sample_c
#define EXTERN
#else
#define EXTERN extern
#endif

EXTERN int x;

Sample.c is only compiled once and it defines the variables. Any file that includes sample.h is only given the "extern" of the variable; it does allocate space for that variable.

样本。c只编译一次,它定义了变量。包含示例的任何文件。h只给出变量的“extern”;它确实为那个变量分配了空间。

When you change the type of x, it will change for everybody. You won't need to remember to change it in the source file and the header file.

当你改变x的类型时,每个人都会改变。您不需要记住在源文件和头文件中更改它。

#5


0  

What about this solution?

这个解决方案呢?

#ifndef VERSION_H
#define VERSION_H

static const char SVER[] = "14.2.1";
static const char AVER[] = "1.1.0.0";

#else

extern static const char SVER[];
extern static const char AVER[];

#endif /*VERSION_H */

The only draw back I see is that the include guard doesn't save you if you include it twice in the same file.

我看到的惟一回退的地方是,如果在同一个文件中包含两次,包含保护就不会保存您。

#1


91  

You should declare the variable in a header file:

您应该在头文件中声明变量:

extern int x;

and then define it in one C file:

然后在一个C文件中定义:

int x;

In C, the difference between a definition and a declaration is that the definition reserves space for the variable, whereas the declaration merely introduces the variable into the symbol table (and will cause the linker to go looking for it when it comes to link time).

在C语言中,定义和声明之间的区别在于定义为变量保留了空间,而声明只是将变量引入符号表(并将导致链接器在链接时查找它)。

#2


29  

You can (should) declare it as extern in a header file, and define it in exactly 1 .c file.

您可以(应该)在头文件中声明它为extern,并在1 .c文件中定义它。

Note that that .c file should also use the header and that the standard pattern looks like:

注意.c文件也应该使用header,标准模式如下:

// file.h
extern int x;  // declaration

// file.c
#include "file.h"
int x = 1;    // definition and re-declaration

#3


9  

If you declare it like

如果你像这样申报的话

int x;

in a header file which is then included in multiple places, you'll end up with multiple instances of x (and potentially compile or link problems).

在一个头文件中,这个头文件被包含在多个地方,最终您将得到x的多个实例(以及潜在的编译或链接问题)。

The correct way to approach this is to have the header file say

处理这个问题的正确方法是让头文件说。

extern int x; /* declared in foo.c */

and then in foo.c you can say

然后在foo。c你可以说

int x; /* exported in foo.h */

THen you can include your header file in as many places as you like.

然后你可以把你的头文件包含在任何你喜欢的地方。

#4


3  

The key is to keep the declarations of the variable in the header file and source file the same.

关键是保持头文件和源文件中变量的声明不变。

I use this trick

我使用这个技巧

------sample.c------
#define sample_c
#include sample.h

(rest of sample .c)

------sample.h------
#ifdef sample_c
#define EXTERN
#else
#define EXTERN extern
#endif

EXTERN int x;

Sample.c is only compiled once and it defines the variables. Any file that includes sample.h is only given the "extern" of the variable; it does allocate space for that variable.

样本。c只编译一次,它定义了变量。包含示例的任何文件。h只给出变量的“extern”;它确实为那个变量分配了空间。

When you change the type of x, it will change for everybody. You won't need to remember to change it in the source file and the header file.

当你改变x的类型时,每个人都会改变。您不需要记住在源文件和头文件中更改它。

#5


0  

What about this solution?

这个解决方案呢?

#ifndef VERSION_H
#define VERSION_H

static const char SVER[] = "14.2.1";
static const char AVER[] = "1.1.0.0";

#else

extern static const char SVER[];
extern static const char AVER[];

#endif /*VERSION_H */

The only draw back I see is that the include guard doesn't save you if you include it twice in the same file.

我看到的惟一回退的地方是,如果在同一个文件中包含两次,包含保护就不会保存您。