The extern keyword is used in C and C++ to declare a variable or function that is defined in another file. This keyword is used to tell the compiler that the variable or function is defined elsewhere and should not be allocated storage in the current file. The connection between the declaration and definition is established at link time.

While this is useful for sharing variables and functions across multiple files, it can be confusing to understand how extern works in practice. The purpose of this post is to provide some sample codes that demonstrate the use of extern in C and C++.

Simple code example

Codes: here

  • Define a variable in extern.c

      // define a global variable
      int global_var = 10;
    
  • Use it in main.c. Here, the variable is declared as extern to indicate that it is defined in another file

      #include <stdio.h>
    
      // this global variable is defined somewhere else that will be linked
      extern int global_var;
    
      int main() {
          printf("Global variable: %d\n", global_var);
          return 0;
      }
    

Calling C functions from C++ programs

Codes: here

  • C functions are defined as usual.

    • header

        #ifndef C_LIBRARY_H
        #define C_LIBRARY_H
      
        void hello_from_c();
        void print_hello();
      
        #endif // C_LIBRARY_H
      
    • codes

        // c library
        #include <stdio.h>
      
        void print_hello() {
            printf("Hello from C!\n");
        }
      
        void hello_from_c() {
            print_hello();
        }
      
    • codes

  • C++ main program

      #include <iostream>
    
      // bring a C function into C++ code
      extern "C"
      {
          void hello_from_c();
      }
    
      int main()
      {
          hello_from_c();
          return 0;
      }
    

Calling C++ functions from C programs

Codes: here

  • C++ functions are defined as usual. But, the function to expose to C should be declared as extern "C"

    • header

        // cpp_library.h
        #ifndef CPP_LIBRARY_H
        #define CPP_LIBRARY_H
      
        #ifdef __cplusplus
        extern "C" {
        #endif
      
        void hello_from_cpp();
      
        #ifdef __cplusplus
        }
        #endif
      
        #endif // CPP_LIBRARY_H
      
      • C++ compiler will see the extern "C" and will not mangle the function name.
      • When included in a C file, the extern "C" will be ignored.
    • codes

        // cpp_library.cpp
        #include <iostream>
        #include "cpp_library.h"
      
        void print_hello_from_cpp()
        {
            std::cout << "Hello from C++!" << std::endl;
        }
      
        void hello_from_cpp()
        {
            print_hello_from_cpp();
        }
      
  • C main program
      // main.c
      #include <stdio.h>
      #include "cpp_library.h"
    
      int main() {
          printf("Calling a C++ function from C:\n");
          hello_from_cpp();
          return 0;
      }
    
  • Makefile: The executable should be linked with the C++ library

      $(TARGET): $(C_OBJECTS) $(CPP_OBJECTS)
          $(CC) -o $@ $^ -lstdc++