user2648701
user2648701

Reputation: 39

one or more multiply defined symbols found c++

I have a header file and 5 different c++ files and I need this header included in all of my c++ files. I did not declare any cpp files with include "x.cpp" Anyone knows how can I fix this?( I have 6 headers and 5 cpp in total so I did not c/p all the code.)

#ifdef _DEBUG
#ifndef _UTIL_H_
#define _UTIL_H_


int LOOPCOUNTER=0;

int loopi;
#define LOOP LOOPCOUNTER++;
#define MARKLOOPS (loopi=LOOPCOUNTER);
#define PRINTLOOPS cout<<LOOPCOUNTER-loopi;
#define PRINTALLLOOPS cout<<LOOPCOUNTER<<endl;

#endif



#endif

and this is the error message:

1>linkedlistc.obj : error LNK2005: "int loopi" (?loopi@@3HA) already defined in arraylistc.obj
1>linkedlistc.obj : error LNK2005: "int LOOPCOUNTER" (?LOOPCOUNTER@@3HA) already defined in arraylistc.obj
1>main.obj : error LNK2005: "int loopi" (?loopi@@3HA) already defined in arraylistc.obj
1>main.obj : error LNK2005: "int LOOPCOUNTER" (?LOOPCOUNTER@@3HA) already defined in arraylistc.obj
1>C:\Users\Eko\Documents\Visual Studio 2010\Projects\mt1\Debug\mt1.exe : fatal error LNK1169: one or more multiply defined symbols found
1>

Upvotes: 1

Views: 7046

Answers (2)

DUman
DUman

Reputation: 2590

Assuming you're getting linker complaints about symbols, your problem is probably that your headers get included multiple times. You should not allow that to happen.

The typical solution is to use an include guard like this:

#ifndnef MYHEADER_H
#define MYHEADER_H

//header code here

#endif

This will ensure that your header only actually gets included once.

Also, you should never #include cpp files, only headers.

On the other hand, if include guards do not help, then you have header files which define symbols as opposed to declaring them. Don't do that. See this question for how to handle global data without defining it in headers.

Upvotes: 0

Unforgiven
Unforgiven

Reputation: 125

I think the header file must have only declarations of variables. You should put the definitions to the appropriate cpp file. Something like this:

// header file
#ifndef _UTIL_H_
#define _UTIL_H_

extern int LOOPCOUNTER;

#endif

// cpp file
// ...
int LOOPCOUNTER = 0;

Upvotes: 2

Related Questions