Re: error LNK2005



You are seeing exactly what should happen. You have made the mistake of declaring
variables in the header file; consequently, each module which includes it gets a new,
conflicting definition of the variable. The first module the linker sees declares the
variable; all subsequent attempts to declare it generate the error you see.

"Assigning values" is a completely unrelated concept to this problem.

Declare the variable once, in the .cpp file associated with the .h file. Declare it as
extern in the header file.
joe

On 17 Feb 2006 09:09:53 -0800, kman_l@xxxxxxxxx wrote:

Can anyone give me some pointers because I get this linking error which
does not seem to make sense to me. I have my arrays defined in a
header file and then included the header file in 5 source files. One
source file at the bottom of the chain is Leg.cpp and it does not
complain but all the other files claim that my arrays are already
defined. I don't understand because these other files don't assign the
arrays any values, but does reference these external variables. And
yes I included the header file in all and declared the arrays as
externs.

here is the building print out:

pathpoint.obj : error LNK2005: "char const * * gpcUnit"
(?gpcUnit@@3PAPBDA) already defined in leg.obj
pathpoint.obj : error LNK2005: "char const * * gpcField"
(?gpcField@@3PAPBDA) already defined in leg.obj
datablock.obj : error LNK2005: "char const * * gpcUnit"
(?gpcUnit@@3PAPBDA) already defined in leg.obj
datablock.obj : error LNK2005: "char const * * gpcField"
(?gpcField@@3PAPBDA) already defined in leg.obj
datacontent.obj : error LNK2005: "char const * * gpcUnit"
(?gpcUnit@@3PAPBDA) already defined in leg.obj
datacontent.obj : error LNK2005: "char const * * gpcField"
(?gpcField@@3PAPBDA) already defined in leg.obj
message.obj : error LNK2005: "char const * * gpcUnit"
(?gpcUnit@@3PAPBDA) already defined in leg.obj
message.obj : error LNK2005: "char const * * gpcField"
(?gpcField@@3PAPBDA) already defined in leg.obj

Any help would be very much appreciated.
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: confused about extern use
    ... going to achieve by declaring it as extern in the header file a.h. ... composed on one or more compilation units. ... Essentially you *declare* the type wherever it is ...
    (comp.lang.c)
  • Re: keyword extern
    ... > declared multiple times using extern in all the files file2.c ... NEVER, ever, define a variable in a header file. ... declare it with the "extern" qualifier in the header file for the ... misuse. ...
    (comp.lang.c)
  • Re: How to declare functions in so to be accessable to applications
    ... >> But the application tells my, that it didn't find the entry point ... > Your header file that the application uses, ... > prototype normally, not as an extern. ... When you declare it extern, ...
    (comp.unix.programmer)
  • Re: keyword extern
    ... my preferred way would be to declare it in all the .c files that ... > need the extern variable rather than put the extern declaration in the ... > header file and include the header file in the c files. ... A header is certainely not the place for an object definition for an ...
    (comp.lang.c)
  • Re: make general windows module
    ... The VB version of these declarations is the "Declare Statement". ... mimicing the behavior of a "windows.h" header file for VB. ... not all Windows API calls can be called from VB as some use reference ...
    (microsoft.public.vb.general.discussion)