10

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

#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
  • no. that's not a declaration, that's a definition (this will cause a linker error). Commented Jun 17, 2014 at 10:25

1 Answer 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.

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:

4
  • As long as you're editing this, don't forget the semicolon at the end.
    – Tom Zych
    Commented Jun 17, 2014 at 10:28
  • Thanks. Didn't realize the OP omitted one in his code.
    – Tamer Tas
    Commented Jun 17, 2014 at 10:29
  • Ah yes, I didn't notice that.
    – Tom Zych
    Commented Jun 17, 2014 at 10:29
  • @TamerTas Thank you for the explanation. So basically 'extern' keyword makes the variable global and so that implies that the keywords 'extern' and 'static' would never be used together as they would mean the opposite things, correct?
    – Ankit
    Commented Jun 17, 2014 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.