[comp.sys.amiga.tech] Lattice and externals

wade@pnet01.cts.com (Wade Bickel) (11/14/89)

  I'm using Lattice C and have declared a pointer with the same name in two
seperate modules.  Neither has the "extern" keyword in front of it, and I'd
like each to be local to its own module (soon almost all of my modules will
need such a pointer, and I'd like to avoid naming confusion).
  When I link the program blink barfs on a "symbol redefined" error.  Should
this be happening?
                                                Thanks -- Wade.

UUCP: {nosc ucsd hplabs!hp-sdd}!crash!pnet01!wade
ARPA: crash!pnet01!wade@nosc.mil
INET: wade@pnet01.cts.com

rap@peck.ardent.com (Rob Peck) (11/15/89)

In article <704@crash.cts.com> wade@pnet01.cts.com (Wade Bickel) writes:
>  I'm using Lattice C and have declared a pointer with the same name in two
>seperate modules.  Neither has the "extern" keyword in front of it, and I'd
>like each to be local to its own module (soon almost all of my modules will
>need such a pointer, and I'd like to avoid naming confusion).
>  When I link the program blink barfs on a "symbol redefined" error.  Should
>this be happening?

In C, any variable name declared in global space becomes a global variable,
with or without the "extern" keyword defined.  The primary purpose for using
an extern is to tell the compiler that a particular label or function call
returns a specific kind of value.  If the compiler sees a reference to
something and it has not yet seen a type declaration for it, the compiler
assumes that the items is, or returns, an integer value (depending on whatever
the size of an int is, 16 or 32 bits).  By declaring something with "extern",
you also specify its type, therefore giving the compiler the information that
it needs to handle the object locally in this single module compile.

When you finally link everything together, think of your complete set of
modules as though they were written as part of a single complete source
program, passed through the compiler as though all modules were typed one
after another.  The linker, at link time, must assign a unique address value
(or stack offset value for automatic... local variables) to each symbol.
Because you have used the same (global) symbol more than once, it correctly
tells you that there has been a symbol redefinition, and cannot resolve the
symbol references to a single location.

Rob Peck

rosenber@ra.abo.fi (Robin Rosenberg INF) (11/15/89)

In article <704@crash.cts.com>, wade@pnet01.cts.com (Wade Bickel) writes:
>   I'm using Lattice C and have declared a pointer with the same name in two
> seperate modules.  Neither has the "extern" keyword in front of it, and I'd
> like each to be local to its own module (soon almost all of my modules will
> need such a pointer, and I'd like to avoid naming confusion).
>   When I link the program blink barfs on a "symbol redefined" error.  Should
> this be happening?
>                                                 Thanks -- Wade.

The keyword 'static' serves three purposes.
- it declares the variable as static (default for global variables)
- hides the name from other modules (what you wanted)
- *guarantees* that the variable is initialized (to zero unless value specified)

----------
Robin Rosenberg

mwm@eris.berkeley.edu (Mike (I'll think of something yet) Meyer) (11/15/89)

In article <704@crash.cts.com> wade@pnet01.cts.com (Wade Bickel) writes:
<  I'm using Lattice C and have declared a pointer with the same name in two
<seperate modules.  Neither has the "extern" keyword in front of it, and I'd
<like each to be local to its own module (soon almost all of my modules will
<need such a pointer, and I'd like to avoid naming confusion).
<  When I link the program blink barfs on a "symbol redefined" error.  Should
<this be happening?

The answer is "it depends." Some C compilers allow you to have
multiple definitions of a variable, which will get treated as one
declaration at compile time. This behavior is not required by ANSI,
but is mentioned as a common extension. Lattice doesn't support that
extension.

On the other hand, that behavior isn't what you want. You want the
scope of the variable to be restricted to the file it's in. This is
where the static keyword comes in. When applied to a definition
outside of any function, it causes the defined object to have file
scope. This applies to functions as well as variables, so that each
modules private functions and variables can be made invisible to
anything outside the file.

Making all functions and variables that don't need linkage outside the
file static is generally a good idea. Not only does it decrease name
space polution for other modules, those objects have the internal name
equivalence properties (31 significant characters, case-sensitive)
instead of the external name equivalence propreties (6 characters,
case-insensitive).

Final note: don't confuse static outside of a function, which controls
scope, with static inside of a function, which controls duration.

	<mike
--
Come rain, come hail, come sleet, come snow		Mike Meyer
You don't like to drive to slow				mwm@berkeley.edu
You're always in the passing lane			ucbvax!mwm
It's enough to drive me out of my brain			mwm@ucbjade.BITNET

gilham@csl.sri.com (Fred Gilham) (11/16/89)

Declare them static:

static char *foo;


This means that they are only visible in the file in which they
appear.
-Fred Gilham  gilham@csl.sri.com

jimm@amiga.UUCP (Jim Mackraz) (11/19/89)

In article <704@crash.cts.com> wade@pnet01.cts.com (Wade Bickel) writes:
)
)  I'm using Lattice C and have declared a pointer with the same name in two
)seperate modules.  Neither has the "extern" keyword in front of it, and I'd
)like each to be local to its own module (soon almost all of my modules will
)need such a pointer, and I'd like to avoid naming confusion).
)  When I link the program blink barfs on a "symbol redefined" error.  Should
)this be happening?
)                                                Thanks -- Wade.

Yes, it's correct.

You must either explicitly declare like-named variables to be the same,
by saying 'extern' with all but one, or different, by using 'static'.

When used on a 'global', static means 'with scope in this source file.'
Just what you want.

	jimm
-- 
--------------------------------------------------	- opinions by me
"This voice console is a *must*.  I press Execute. 
 `Hello, I know that you've been feeling tired.
  I bring you love and deeper understanding.' "		-lyrics by Kate Bush