Categories
c extern global-variables

How do I use extern to share variables between source files?

1166

I know that global variables in C sometimes have the extern keyword. What is an extern variable? What is the declaration like? What is its scope?

This is related to sharing variables across source files, but how does that work precisely? Where do I use extern?

0

    137

    An extern variable is a declaration (thanks to sbi for the correction) of a variable which is defined in another translation unit. That means the storage for the variable is allocated in another file.

    Say you have two .c-files test1.c and test2.c. If you define a global variable int test1_var; in test1.c and you’d like to access this variable in test2.c you have to use extern int test1_var; in test2.c.

    Complete sample:

    $ cat test1.c 
    int test1_var = 5;
    $ cat test2.c
    #include <stdio.h>
    
    extern int test1_var;
    
    int main(void) {
        printf("test1_var = %d\n", test1_var);
        return 0;
    }
    $ gcc test1.c test2.c -o test
    $ ./test
    test1_var = 5
    

    4

    • 24

      There’s no “pseudo-definitions”. It’s a declaration.

      – sbi

      Sep 16, 2009 at 14:18

    • 3

      In the above example, if I change the extern int test1_var; to int test1_var;, the linker (gcc 5.4.0) still passes. So, is extern really needed in this case?

      – radiohead

      Mar 24, 2018 at 3:15


    • 2

      @radiohead: In my answer, you will find the information that dropping the extern is a common extension that often works — and specifically works with GCC (but GCC is far from being the only compiler that supports it; it is prevalent on Unix systems). You can look for “J.5.11” or the section “Not so good way” in my answer (I know — it is long) and the text near that explains it (or tries to do so).

      Jun 16, 2018 at 19:44


    • An extern declaration certainly doesn’t have to be defined in another translation unit (and commonly is not). In fact, declaration and definition can be one and the same.

      Dec 28, 2019 at 0:35

    42

    Extern is the keyword you use to declare that the variable itself resides in another translation unit.

    So you can decide to use a variable in a translation unit and then access it from another one, then in the second one you declare it as extern and the symbol will be resolved by the linker.

    If you don’t declare it as extern you’ll get 2 variables named the same but not related at all, and an error of multiple definitions of the variable.

    1

    • 5

      In other words the translation unit where extern is used knows about this variable, its type etc. and hence allows the source code in the underlying logic to use it, but it does not allocate the variable, another translation unit will do that. If both translation units were to declare the variable normally, there would be effectily two physical locations for the variable, with the associated “wrong” references within the compiled code, and with the resulting ambiguity for the linker.

      – mjv

      Sep 16, 2009 at 14:19