ARM Technical Support Knowledge Articles

How does the compiler handle multiple definitions of global variables?

Applies to: DS-5, RealView Development Suite (RVDS)


The way in which C and C++ handle multiple definitions of the same global variable is very different to each other.

The C standard1 implements a concept named tentative definitions according to which:

"A declaration of an identifier for an object that has file scope without an initializer, and without a storage-class specifier or with the storage-class specifier static, constitutes a tentative definition".

Multiple declarations of the same global variable are treated as tentative definitions and integrated into a single definition at the end of the C compilation process.

For example, consider the following source code:

#include <stdio.h>

int bar;     // Tentative definition 1
int bar;     // Tentative definition 2

int fn_dummy(int a, int b)
    return (a+b+bar);

int bar=5;   // Definition
int bar;     // Tentative definition 3

int fn_dummy2(int a)

    return (bar-a);

Here the three declarations of bar are considered to be tentative definitions.  These are integrated into the one actual definition, which has a value of 5. If there is no actual definition then the compiler initializes the value of the global variable to zero.

The C++ standard2 however, does not allow tentative definitions and multiple global variable declarations are therefore reported as errors by the C++ compiler.  

[Section 6.9.2 - C Standard Specification ISO/IEC 9899:TC2]
2 [Annex C.1.2, Clause 3.1 - C++ International Standard ISO/IEC 14882-1998]

Article last edited on: 2015-04-23 15:12:34

Rate this article

Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential