thanks, as you stated,
I remember in C the defintion of an object can ONLY occur ONLY ONCE.
but if I write extern int var1 = 0x0bad ; in one.c,
and write extern int var1 = 0xdead; in another.c
the vc c compiler will happy accept it.
since you think
how to explain it?
thanks
There are a lot of technical details about different models of linking
that is used by the linkers of different implementations on different
platforms. Some of them will allow multiple definitions of an
external symbol, as long as no more than one of them has an
initializer.
The C standard requires that any object or function with external
linkage that is not actually referenced in the program must have
exactly 0 or 1 definitions somewhere in the program. Any object
or function with external linkage that is actually referenced in a
program must have exactly 1 definition, no more and no less.
A violation of this requirement causes undefined behavior, and
compilers are never required to diagnose undefined behavior.
Any compiler should be able to compile the two source files with two
external definitions of 'var1', both with initializers. The error
should come when the two object files are linked together to make the
program.
I don't know of any version of Microsoft's Visual C++, or any other C
compiler for that matter, that will not generate an error if those two
source files are put into a single program.
Here is what I got from Visual C++ 6.0:
--------------------Configuration: multi - Win32
Debug--------------------
Linking...
multi2.obj : error LNK2005: _var1 already defined in multi1.obj
multi___Win32_Debug/multi.exe : fatal error LNK1169: one or more
multiply defined symbols found
Error executing link.exe.
multi.exe - 2 error(s), 0 warning(s)
And here is what I got from the Visual C++ 2005 Express Edition Beta:
------ Build started: Project: multi, Configuration: Debug Win32
------
Compiling...
multi2.c
Linking...
multi1.obj : error LNK2005: _var1 already defined in multi2.obj
Debug/multi.exe : fatal error LNK1169: one or more multiply defined
symbols found
Build log was saved at "file://c:\Program Files\Microsoft Visual
Studio 8\projects\multi\multi\Debug\BuildLog.htm"
multi - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========