Solved

Linkage problems using visual c++ tool

Posted on 2004-04-05
6
331 Views
Last Modified: 2012-05-04
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
0
Comment
Question by:haboles
6 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 10756192
Global variables should be defined by the following way:

// global.h
#ifnfef GLOBAL_H
#define GLOBAL_H

extern int VAR;

// other global variables, all with extern keyword

#endif

// cpp file (define each global variable only in one cpp file, use it in all cpp files where necessary)
#include "Global.h"

int VAR;


0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10756207
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.


0
 
LVL 17

Expert Comment

by:rstaveley
ID: 10756642
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<--------
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10758019
>> 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

0
 
LVL 17

Expert Comment

by:rstaveley
ID: 10758180
> 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.
0
 

Author Comment

by:haboles
ID: 10758453
thanks all for the quick answers.
good to know that i have place to go now when i have c/c++ question.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Quicksort a dynamic deque 33 66
Writing a parser for java language 4 71
Compile GLUT with Visual Studio 2015 1 111
Socket Programming (Unix) 8 119
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now