Ankur
Ankur

Reputation: 11739

Use of "extern" storage class specifier in C

How does the following example usage of extern specifer behave.

We have a global variable int x both in files one.c and two.c We want to use these in three.c so have declared this variable in three.c as

extern int x;

What would happen when we compile and link these files?

My answer is: compilation of all these files should succeed, however the linker should flag an error at linking, due to multiple declarations of x. Would there be any difference in behavior in C++ ?

Is these any way to refer to int x (in three.c) simultaneously from both files, in C and C++. In C++, I guess we can use namespaces to acheive this. Right?

Upvotes: 1

Views: 1844

Answers (5)

Jay D
Jay D

Reputation: 3307

Remember that you can NOT extern a global static variable.. !!

Upvotes: 0

Chris Lutz
Chris Lutz

Reputation: 75399

In C, you could do this:

// one.c
static int x;
int *one_x = &x;

// two.c
static int x;
int *two_x = &x;

// three.c
extern int *one_x;
extern int *two_x;

Now you can refer unambiguously to the x in file one.c or the x in file two.c from the file three.c.

However, this might be a bit more effort than it's worth. Perhaps you should be coming up with more descriptive names for your global variables instead of toying around with theoretical ways to circumvent C's single global namespace.

Upvotes: 1

Adam Rosenfield
Adam Rosenfield

Reputation: 400244

By default, global variables have external linkage, which means that they can be used by other source files (or "translation units"). If you instead declare your global variables with the static keyword, they will have internal linkage, meaning they will not be usable by other source files.

For variables with external linkage, you can't have multiple variables with the same name, or the linker will complain. You can have two variables with the same name, though, as long as at least one has internal linkage, and of course you can't reference both of them in the same source file.

An extern declaration is just saying to the compiler "here is the name of some variable with external linkage defined in another translation unit," allowing you to refer to that variable.

C++ is exactly the same, except for the addition of namespaces. If global variables are put inside a namespace, then they can have the same name without linker errors, provided they are in different namespaces. Of course, all references to those variables then have to either refer to the full name namespace::var_name, or use a using declaration to establish a local namespace context.

C++ also has anonymous namespaces, which are entirely equivalent to using the static keyword for global variables in C: all variables and functions declared inside an anonymous namespace have internal linkage.

So, to answer your original question, you are right -- compilation would succeed, but linking would fail, due to multiple definitions of the variable x with external linkage (specifically, from the translation units one.c and two.c).

From three.c, there is no way to refer simultaneously to both variables x. You'll need to rename x in one or both modules, or switch to C++ and put at least one x inside a namespace.

Upvotes: 6

Pascal Cuoq
Pascal Cuoq

Reputation: 80276

You might be interested by the answers to this question.

Summary: the linker may or may not fail to link the file. It depends on the initialization of the variable. It will definitely fail if the variable has different initializations in different files.

Upvotes: 0

Charles Salvia
Charles Salvia

Reputation: 53289

To avoid generating duplicate symbols, you should declare extern int x; in a single header file (a .h file), and then have all .c files which will use x #include that header file, and define or initialize int x; in one of the .c files.

Upvotes: 0

Related Questions