How to use extern to share variables between source files in C/C++?

To share variables between source files in C/C++ using the extern keyword, follow these steps:

Step-by-Step Guide

  1. Define the Variable in One Source File
    Declare and initialize the variable once in a single .c or .cpp file (without extern). This allocates memory for the variable.
   // file1.c
   int sharedVar = 42;  // Definition (allocates memory)
  1. Declare the Variable as extern in a Header File
    Use extern in a header file to indicate the variable exists elsewhere. This is a declaration, not a definition.
   // shared.h
   #ifndef SHARED_H
   #define SHARED_H

   extern int sharedVar;  // Declaration (no memory allocated)

   #endif
  1. Include the Header in Other Files
    Include the header in any file needing access to the shared variable.
   // file2.c
   #include "shared.h"
   #include <stdio.h>

   void printVar() {
       printf("sharedVar = %d\n", sharedVar);  // Uses sharedVar from file1.c
   }
  1. Compile and Link All Files
    Ensure all source files are compiled and linked together.
   gcc file1.c file2.c main.c -o program

Key Rules

  • One Definition Rule (ODR): The variable must be defined exactly once in one source file.
  • Declaration vs. Definition:
  • Use extern in headers for declarations (promises the variable exists elsewhere).
  • Omit extern in the source file for the definition (allocates memory).

C++ Specifics

  • For const Variables:
    In C++, const variables have internal linkage by default. To share them:
  // shared.hpp
  extern const int sharedConst;  // Declaration

  // file1.cpp
  extern const int sharedConst = 100;  // Definition (requires 'extern')
  • Namespaces:
    Declare and define variables in namespaces properly:
  // shared.hpp
  namespace Shared {
      extern int sharedVar;
  }

  // file1.cpp
  namespace Shared {
      int sharedVar = 42;  // Definition
  }

Example Structure

project/
├── shared.h    // extern declarations
├── file1.c     // variable definition
├── file2.c     // uses the variable
└── main.c      // main program

Common Pitfalls

  1. Multiple Definitions: Defining the variable in multiple files causes linker errors.
  2. Missing extern in Headers: Leads to separate variables in each file (violates ODR).
  3. Forgetting to Link Files: Results in “undefined reference” errors.

Testing the Example

file1.c

#include "shared.h"
int sharedVar = 42;  // Definition

file2.c

#include "shared.h"
#include <stdio.h>

void printVar() {
    printf("sharedVar = %d\n", sharedVar);
}

main.c

#include "shared.h"
#include <stdio.h>

void printVar();  // Function declaration

int main() {
    printf("sharedVar in main: %d\n", sharedVar);
    printVar();
    return 0;
}

Compile and Run

gcc file1.c file2.c main.c -o program && ./program

Output

sharedVar in main: 42
sharedVar = 42

Conclusion

The extern keyword enables safe sharing of variables across files by separating declaration (headers) and definition (one source file). Adhere to the One Definition Rule and proper linkage to avoid errors.

Leave a Reply

Your email address will not be published. Required fields are marked *