tags:

views:

146

answers:

2

Hi,

I am beginning to question the usefulness of "extern" keyword which is used to access variables/functions in other modules(in other files). Aren't we doing the same thing when we are using #include preprocessor to import function/variables definitions?

EDIT: Maybe I should add this to the question: ... using # include preprocessor to import a header file with variables/functions prototypes.

+1  A: 

No. The #include is a preprocessor command that says "put all of the text from this other file right here". So, all of the functions and variables in the included file are defined in the current file.

jcopenha
+11  A: 

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.

Michael
But can't you still access int foo without declaring with extern int foo as long as you include the header file containing its definition?
Midnight Blue
As jcopenha mentioned, #include just inserts the text from the included file into the source file - the compiler isn't really aware of include files and doesn't treat them specially. So if you have A.c, B.c, and C.c, each with "int foo" and you link them together, how do you resolve foo? It exists in 3 different places. extern just means, the symbol exists somewhere else. You can use it, but someone else is responsible for creating it.
Michael
You will find that for functions where there is only one instance of foo_fun() programs with modules that call foo_fun() will work with or without the extern. So in those cases you should try to be clear about it. When you get into a shared global variable you need to be really clear, some compilers figure it out, some do not.
dwelch
@Midnight Blue: I think you're confusing a coding convention with the behavior required by the C specification. It's common practice to put `extern` declarations in the header file for a code module and `#include` the header to get those declarations instead of declaring them yourself. It's the `extern` in the header that you `#include` that provides access to the variable, not the `#include` itself.
Michael Carman
I've seen declarations that go like `Type array[];` in headers. But there is a subtle difference of that to `extern Type array[];`! The former is a *tentative definition*, which will be treated like `Type array[1];` when there is no definition including the size until the end of the translation unit. But if you put `extern` before it, you just say "The size is given elsewhere", and that's what is most often wanted.
Johannes Schaub - litb