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.
#include "lib.h"
void main(void)
{
// Code here
var++;
}
#include <avr/io.h>
#include "lib.h"
void light_led(void)
{
// Code here
}
volatile int var;
void light_led(void);
Is this the correct way of making and including your own custom-made libraries?
ANSWER. Yes. Although this is not necessarily recommended, it can be easily accomplished with the correct set of macros and a header file. Typically, you should declare variables in C files and create extern definitions for them in header files.
The clean, reliable way to declare and define global variables is to use a header file to contain an extern declaration of the variable. The header is included by the one source file that defines the variable and by all the source files that reference the variable.
Of course, a global variable can only be used in an executable statement after it has been declared. Hence, the natural place to put global variable declaration statements is before any function definitions: i.e., right at the beginning of the program.
The keyword 'Global' is also used to create or declare a global variable inside a function. Usually, when you create a variable inside a function (a local variable), it can only be used within that function.
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.
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
.
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.
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.
According to 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).
For more information check below:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With