在头文件或C源文件中声明全局变量

时间:2020-12-14 16:49:54

I am kind of confused about the whole including header files and declaration of variables.

我对整个包括头文件和变量声明感到困惑。

Files I am using are: main.c, lib.h and lib.c.

我使用的文件是:main.c,lib.h和lib.c.

main.c

#include "lib.h"

void main(void)
{
    // Code here
    var++;
}

lib.c

#include <avr/io.h>
#include "lib.h"

void light_led(void)
{
    // Code here
}

lib.h

volatile int var;

void light_led(void);

Is this the correct way of making and including your own custom-made libraries?

这是制作和包含您自己的定制库的正确方法吗?

1 个解决方案

#1


9  

You should use extern keyword for such cases and not define global variables in headers. Otherwise the linker will throw errors when operating on your header files.

您应该对此类情况使用extern关键字,而不是在标头中定义全局变量。否则,链接器将在对头文件进行操作时抛出错误。

lib.c

#include <avr/io.h>
#include "lib.h"

volatile int var;

void light_led(void)
{
//code here
}

lib.h

extern volatile int var;

void light_led(void);

This way you'll be declaring the global variable in headers and actually defining it in the appropriate source file lib.c.

这样你就可以在头文件中声明全局变量,并在相应的源文件lib.c中实际定义它。

Note: Notice the difference between declaring and defining a variable. extern keyword allows the variable to be declared in advance without being defined. Had you not defined the variable in lib.c, there would be an error when you tried to use this variable. Since, it is only declared but, not actually defined.

注意:请注意声明和定义变量之间的区别。 extern关键字允许在不定义的情况下提前声明变量。如果您没有在lib.c中定义变量,则在尝试使用此变量时会出现错误。因为,它只是声明但实际上没有定义。

Edit: The whole purpose of static is to declare that a variable is private to the source file that is declared in. Since, extern does the opposite by linking a variable defined in another source file, it defeats the purpose of static. extern says the variable has external linkage static says the variable has internal linkage. An identifier can't have both internal and external linkage.

编辑:静态的全部目的是声明变量对于声明的源文件是私有的。因为,extern通过链接另一个源文件中定义的变量来执行相反的操作,所以它违背了静态的目的。 extern说变量有外部连接静态表示变量有内部连接。标识符不能同时具有内部和外部链接。

According to MSND:

根据MSND:

When modifying a variable, the static keyword specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends) and initializes it to 0 unless another value is specified. When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared).

修改变量时,static关键字指定变量具有静态持续时间(在程序开始时分配,在程序结束时释放)并将其初始化为0,除非指定了另一个值。在文件范围内修改变量或函数时,static关键字指定变量或函数具有内部链接(其名称在声明它的文件外部不可见)。

For more information check below:

有关更多信息,请查看以下

#1


9  

You should use extern keyword for such cases and not define global variables in headers. Otherwise the linker will throw errors when operating on your header files.

您应该对此类情况使用extern关键字,而不是在标头中定义全局变量。否则,链接器将在对头文件进行操作时抛出错误。

lib.c

#include <avr/io.h>
#include "lib.h"

volatile int var;

void light_led(void)
{
//code here
}

lib.h

extern volatile int var;

void light_led(void);

This way you'll be declaring the global variable in headers and actually defining it in the appropriate source file lib.c.

这样你就可以在头文件中声明全局变量,并在相应的源文件lib.c中实际定义它。

Note: Notice the difference between declaring and defining a variable. extern keyword allows the variable to be declared in advance without being defined. Had you not defined the variable in lib.c, there would be an error when you tried to use this variable. Since, it is only declared but, not actually defined.

注意:请注意声明和定义变量之间的区别。 extern关键字允许在不定义的情况下提前声明变量。如果您没有在lib.c中定义变量,则在尝试使用此变量时会出现错误。因为,它只是声明但实际上没有定义。

Edit: The whole purpose of static is to declare that a variable is private to the source file that is declared in. Since, extern does the opposite by linking a variable defined in another source file, it defeats the purpose of static. extern says the variable has external linkage static says the variable has internal linkage. An identifier can't have both internal and external linkage.

编辑:静态的全部目的是声明变量对于声明的源文件是私有的。因为,extern通过链接另一个源文件中定义的变量来执行相反的操作,所以它违背了静态的目的。 extern说变量有外部连接静态表示变量有内部连接。标识符不能同时具有内部和外部链接。

According to MSND:

根据MSND:

When modifying a variable, the static keyword specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends) and initializes it to 0 unless another value is specified. When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared).

修改变量时,static关键字指定变量具有静态持续时间(在程序开始时分配,在程序结束时释放)并将其初始化为0,除非指定了另一个值。在文件范围内修改变量或函数时,static关键字指定变量或函数具有内部链接(其名称在声明它的文件外部不可见)。

For more information check below:

有关更多信息,请查看以下