1

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>
SKi
  • 8,007
  • 2
  • 26
  • 57
user2648701
  • 39
  • 1
  • 2
  • 5
  • Including the exact error messages and the offending declarations would be helpful. – Captain Obvlious Aug 04 '13 at 15:23
  • You've apparently defined one or more symbols in a header, so including that header more than once results in multiple definitions. Include guards won't help -- they prevent including a header more than once in the same source file, but won't cure linking problems like you have here. – Jerry Coffin Aug 04 '13 at 15:23
  • Names that begin with an underscore followed by a capital letter (_UTIL_H_) and names that contain two consecutive underscores are reserved to the implementation. Don't use them. – Pete Becker Aug 04 '13 at 15:41

2 Answers2

2

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;
Unforgiven
  • 125
  • 6
  • Names that begin with an underscore followed by a capital letter (`_UTIL_H_`) and names that contain two consecutive underscores are reserved to the implementation. Don't use them. – Pete Becker Aug 04 '13 at 15:37
  • @James Sure, but it depends on user's goal. Static variables will be "local" for each file, won't they? – Unforgiven Aug 04 '13 at 15:39
  • @PeteBecker Thanks, I just copy-pasted them from the first message) – Unforgiven Aug 04 '13 at 15:40
  • @Unforgiven - whoops, I missed it in the original message. – Pete Becker Aug 04 '13 at 15:41
  • @Unforgiven Yes, static variables have internal linkage. If you declare a static in a header file, each source file that includes that header will obtain its own instance of that variable. I don't know what the OP's use case is. I just thought I'd mention it for completeness sake. :) –  Aug 04 '13 at 19:00
0

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.

Community
  • 1
  • 1
DUman
  • 2,560
  • 13
  • 16
  • Names that begin with an underscore followed by a capital letter (_MYHEADER_H) and names that contain two consecutive underscores are reserved to the implementation. Don't use them. – Pete Becker Aug 04 '13 at 15:38
  • @PeteBecker Edited, I have a bad habit of forgetting about single-underscore names. – DUman Aug 04 '13 at 15:45
  • @user1264727 - lots of people do, which is why I have a fairly canned reminder... – Pete Becker Aug 04 '13 at 15:46