What’s the difference between using extern and #including header files?

extern is needed because it declares that the symbol exists and is of a certain type, and does not allocate storage for it.

If you do:

int foo;

In a header file that is shared between several source files, you will get a linker error because each source would have its own copy of foo created and the linker will be unable to resolve the symbol.

Instead, if you have:

extern int foo;

In the header, it would declare a symbol that is defined elsewhere in each source file.

One (and only one) source file would contain

int foo;

which creates a single instance of foo for the linker to resolve.

Leave a Comment