Let's say I have two C modules: The first module is a "file1.c" and "file1.h". The second module is "file2.c" and "file2.h". The first source file contains a variable called sharedVariable
.
file1.c
/* Includes */
#include "stdlib.h"
int sharedVariable; // Global variable
/* Some C code */
file2.c
/* Includes */
#include "stdlib.h"
extern int sharedVariable; // Global variable
/* Some C code */
My question is: To make sharedVariable
visible in file2.c, should I add #include "file1.h"
? If yes, should I put something in it or I just leave it empty? If adding the header of the first file is unnecessary, how could the compiler identify the origin of sharedVariable
?
EDIT:
I am looking for good practices to share a variable between two files. One of them is the owner and the other one is just a kind of reader. I used to put getters and setters in the owner file (setters are static functions) and expose the getter to the other one. But I am just curious about extern in C. Is it a good practice to use it instead?