Help needed with #include problems

der Mouse mouse at mcgill-vision.UUCP
Wed May 18 18:23:48 AEST 1988


In article <28400001 at ntvax>, john at ntvax.UUCP writes:
> I am using Turboc 1.5 and am having problems linking files.  I am
> writing a large application (over 64k) and am having trouble breaking
> up the file.  [...] The compile goes ok but the linker gives me the
> following error - "variable name here" is duplicated in module
> "source module name here".

It sounds as though you have run into the difference between the
"def/ref" model of shared variables and the "common" model.  The
"common" model is the one UNIX has historically used; in this one, a
variable can be defined in multiple files, as long as it is initialized
at most once.  In the "def/ref" model, a variable must be defined
exactly once.  Definition-by-example of terms:

reference	extern int i;
definition	int i;
initialization	int i = 5;

Under UNIX, you could have multiple definitions and the loader would
merge them, provided at most one of them was initialized.  On other
systems (apparently Turbo 1.5 falls into this category), we have the
"def/ref" (definition/reference) model, where only one definition is
permitted and other files must contain merely references.  The K&R
definition of C permits either model; presumably dpANS C does as well,
though you would do well to use just the def/ref model when writing new
code, because it works on both sorts of systems.

Your header file should have externs; each variable must have exactly
one definition, somewhere in one of the C files.  (Which C file defines
which variable doesn't matter from the linker's point of view, though
good style of course says they should be grouped reasonably.  I
generally have one file which contains nothing but variable
definitions.)

					der Mouse

			uucp: mouse at mcgill-vision.uucp
			arpa: mouse at larry.mcrcim.mcgill.edu



More information about the Comp.lang.c mailing list