Date: Fri, 1 Apr 94 11:55:08 EST From: ajay_kamdar AT warren DOT mentorg DOT com To: djgpp AT sun DOT soe DOT clarkson DOT edu, WKIM AT vms DOT cis DOT pitt DOT edu Subject: Re: storage allocation of global var $ For example, two files, main.c and test.c, both have the following line: $ $ #include "test.h" $ $ and, a global variable is declared in test.h: $ $ int global_var; $ $ I compiled *.c separetely and then linked them, (with -Wall compile), $ and I didn't have any warning message for double declarations. $ $ My question is, $ $ 1. Is there no storage loss? (i.e., Is the storage for 'global_var' $ allocated only once and shared by both main.c and test.c?) $ $ 2. If there is no storage loss, the linker handled this and merged $ the storage allocation for the same symbol, didn't it. (obviously) According to the C++ Annotated Reference Manual (ARM), this program should not link successfully. From section 3.1 (pg. 13) of the ARM: "A declaration is a definition unless it ...., it contains the extern specifier and no initializer or function body .....". Thus global_var's declaration is a definition since it does not contain an extern specifier. Continuing further, section 3.1 (pg. 14) of the ARM says : "There must be exactly one definition of each object, ..... used in a program." By defining global_var in two translation units, the above program violates the ODR (one definition rule). Further on the same page, in the commentary, the ARM notes that C++ is different from ANSI C by considering "int a;" (with no extern specifier) a definition. Thus in this particular case, djgpp's linker appears to be following ANSI C rules rather than C++ rules about what constitutes a definition. To verify this theoritical analysis, I tried this example on my Sparc with Sun 2.1 and 3.0 C++ compilers (both are cfront based), with cc (which is a K&R C compiler), and with acc (which is Sun's ANSI C compiler). Both the C++ compilers compiled the individual translation units (as they should have) and failed to link because global_var was multiply defined (again as they should have). On the other hand, both the K&R and ANSI C compilers compiled and linked this program without any problem. About the -Wall option: A warning should not be expected from the compiler in this particular case while compiling the individual translation units since there is nothing wrong with the syntax used in the translation units per se. Hope this helps. - Ajay