How to Use extern to Share Variables between Source Files

Global variables are variables that are declared outside of any function. They can be accessed and modified from any part of the program. However, when working with multiple source files in C or C++, you may encounter situations where you need to share variables across these files. This is where the extern keyword comes in handy.

What is an extern variable?

An extern variable is a global variable that is declared in one file and used in another file. The extern keyword is used to indicate to the compiler that the variable is defined in a different file, and its actual definition will be resolved during the linking phase of the compilation process.

How to declare an extern variable

To declare an extern variable, you need to use the extern keyword followed by the variable declaration. This declaration is typically placed in a header file that is included in multiple source files. Here's an example:

            
                // header.h
                extern int sharedVariable;
            
        

In this example, the extern keyword is used to declare a variable named sharedVariable. The actual definition of this variable will be provided in another source file.

How to define an extern variable

The definition of an extern variable should be provided in one of the source files. This is where the variable is allocated memory. Here's an example:

            
                // source1.c
                int sharedVariable;
            
        

In this example, the sharedVariable variable is defined without the extern keyword. This tells the compiler to allocate memory for the variable.

It's important to note that the definition of the extern variable should appear only once in the entire program. If multiple source files contain the definition, it will lead to a linker error.

How to use an extern variable

Once the extern variable is declared and defined, you can use it in any part of the program. Simply include the header file that contains the extern declaration, and you'll be able to access the variable. Here's an example:

            
                // source2.c
                #include "header.h"
                
                void doSomething() {
                    sharedVariable = 10;
                }
            
        

In this example, the header.h file is included, providing the extern declaration for the sharedVariable. The doSomething() function then modifies the value of the sharedVariable.

Scope of extern variables

The scope of an extern variable is global. It can be accessed from any part of the program, including other source files where the variable is declared with the extern keyword.

Example: Sharing a variable between source files

Let's consider a scenario where you have two source files: source1.c and source2.c. You want to share a variable named counter between these files. Here's how you can achieve that using the extern keyword:

            
                // source1.c
                #include "header.h"
                
                int counter = 0;
                
                void incrementCounter() {
                    counter++;
                }

                // source2.c
                #include "header.h"
                
                extern int counter;

                void printCounter() {
                    printf("Counter value: %d\n", counter);
                }
            
        

In this example, the extern declaration of the counter variable is placed in both source1.c and source2.c. The actual definition of the variable is provided in source1.c. The incrementCounter() function increments the value of the counter variable, while the printCounter() function prints its value.

Conclusion

The extern keyword is a useful tool when working with multiple source files in C or C++. It allows you to share variables across these files by declaring them in a header file and defining them in one of the source files. Remember to include the header file wherever you need to access the extern variable, and ensure that the variable is defined only once in the entire program.