Link to home
Start Free TrialLog in
Avatar of haboles
haboles

asked on

Linkage problems using visual c++ tool

I was trying to connect 2 projects - one in C and the other in C++ to one C++ project.
i converted all the extensions of the C files into cpp ones.
Then , when i tryied to linkage all the project together i got more then 900 errors in the form of

A.obj : error LNK2005 : "unsigned char VAR" (?VAR@3PAPAEA) already defined in B.obj

all the A files that gives the linkage errors are the c files converted into cpp ones.
all the VAR variables that gives the linkage errors are in the file global.h which was belong to the former C project.
i suspected that this is an "IFNDEF" problem in the global.h problems but i don't think this is the case.

any advice of how to solve this errors will be highly appreciated since this question concerned to my final project which i need to finish very soon so thanks in advance
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AlexFM
AlexFM

Global.h file contains all global variables with extern keyword. This doesn't create variable, only defines it's type. To create global variable, it should be written without extern keyword:

extern int VAR;    // like forward declaration

int VAR;    // variable is created

Create variable in only one cpp file. Use it in any cpp file - include "Global.h" to it and compiler is happy.


For the benefit of the C file (assuming you are compiling it as C rather than C++), you should use the following to ensure that the symbol doesn't get C++ name mangling:

header.h
--------8<--------
extern
#ifdef __cplusplus
          "C"
#endif
                int VAR;
--------8<--------

source.c/.cpp
--------8<--------
#ifdef __cplusplus
extern "C"
#endif
                int VAR = 0;
--------8<--------
>> i converted all the extensions of the C files into cpp ones.

Because of this all sources are C++ sources now.

Name mangling is an issue of functions only.

Regards, Alex

> Name mangling is an issue of functions only.

Stone me you're right... and structs, classes and static data members in classes I now see. Thanks for the correction, Alex.
Avatar of haboles

ASKER

thanks all for the quick answers.
good to know that i have place to go now when i have c/c++ question.