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>
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;
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.
Related
I know this is a common problem, but I am pretty sure there is no error with how I include the files.
I'll give you the basic files.
Main.cpp:
#include "GameState.h"
#inlcude "Timer.h"
int main ( int argc, char** argv ) {
GameState.h:
#pragma once
#include "Character.h"
Character.h:
#pragma once
#include "Setup.h"
Setup.h:
#pragma once
#include "SDL.h"
#include "SDL_main.h"
#include "SDL_image.h"
Error report:
Error 1 error LNK2005: "void __cdecl apply_surface(int,int,struct SDL_Surface *,struct SDL_Surface *,struct SDL_Rect *)" (?apply_surface##YAXHHPAUSDL_Surface##0PAUSDL_Rect###Z) already defined in Character.obj C:\Users\Jim\Documents\C++\herorpg\herorpg\Main.obj
Error 2 error LNK2005: "bool __cdecl init(struct SDL_Surface * &)" (?init##YA_NAAPAUSDL_Surface###Z) already defined in Character.obj C:\Users\Jim\Documents\C++\herorpg\herorpg\Main.obj
Error 3 error LNK2005: "bool __cdecl load_files(struct SDL_Surface * * const)" (?load_files##YA_NQAPAUSDL_Surface###Z) already defined in Character.obj C:\Users\Jim\Documents\C++\herorpg\herorpg\Main.obj
Error 4 error LNK2005: "struct SDL_Surface * __cdecl load_image(char *)" (?load_image##YAPAUSDL_Surface##PAD#Z) already defined in Character.obj C:\Users\Jim\Documents\C++\herorpg\herorpg\Main.obj
Error 6 error LNK1169: one or more multiply defined symbols found C:\Users\Jim\Documents\C++\herorpg\Debug\herorpg.exe
Is there anything wrong with what I'm including? If you think more information is required, I'll post the full code. Just seemed like a nuisance before.
C++ has a rule called the one definition rule. Among other things, this rule specifies that there cannot be multiple definitions of a function across your program. You can't have two translation units that both define a function, otherwise you break this rule. You can think of a translation unit as a .cpp file with all of its headers included into the appropriate places.
So if you have a header file, foo.h, that looks like this:
#ifndef FOO_H
#define FOO_H
int foo() { return 5; }
#endif
And then you include this header in two or more .cpp files, each of the translation units will have their own definition. This violates the one definition rule.
To fix this, your header should give a function declaration like so:
#ifndef FOO_H
#define FOO_H
int foo();
#endif
Then, in the corresponding foo.cpp file, give a definition for the function:
#include "foo.h"
int foo() { return 5; }
This means that only the foo.cpp translation unit will have a definition of foo. Any uses of foo in other translation units will be linked to that definition.
An alternative is to declare the function as inline, as in:
#ifndef FOO_H
#define FOO_H
inline int foo() { return 5; }
#endif
The reason this is allowed is because each translation must be able to see the definition of such a function to be able to inline it. However, I don't recommend using inline all willy-nilly.
Linker errors are not caused by #include errors. Linker errors usually happens when the compilers can't find the definition of something. Or if it finds multiple definitions ( such as in this case )
Check if you are linking with multiple SDL libraries or if you have defined the functions yourself somwhere in the code
possible reason:
define functions in header files. functions should only be defined in .cpp file.
looped header files including. like: a.h includes b.h, b.h includes c.h, and c.h includes a.h. Sometime the looped including is not obvious, but it does happen. "#pragma once" can only prevents one header file from being included more than once, but cannot prevent looped including. To solve this problem, using "forward declaration" to replace some #include statements.
some links about forward declaration:
http://en.wikipedia.org/wiki/Forward_declaration
When can I use a forward declaration?
http://msdn.microsoft.com/en-us/library/f432x8c6(v=vs.80).aspx
Hi I'm currently working on a program in C++ and my IDE is obviously VC++ and I came across this linker error.
error LNK2005: "class Graphics TheGraphics" (?TheGraphics##3VGraphics##A) already defined in Game.obj
I looked up how to fix it but none of the links got it fixed. So I thought I ask you all myself.
These are all my files
Game.h
Graphics.h
Inc.h
Main.h
Game.cpp
Graphics.cpp
Main.cpp
WndProc.cpp
and in all of the header files I have the
#ifndef ...
#define...
..
#endif
and I also have some includes in the header files.
in Inc.h I have this between the #ifndef and #define
#include <Windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#include "Game.h"
#include "Graphics.h"
in Main.h I have this between the #ifndef and #define
#include "Inc.h"
and I've also created to objects Game and Graphics objects
extern Game TheGame;
extern Graphics TheGraphics
I've also declared 1 function
LRESULT CALLBACK WndProc(HWND hWnd,
UINT Msg,
WPARAM wParam,
LPARAM lParam);
That's Main.h
In Game.h and Graphics.h I have this before the #ifndef and #define
#include "Main.h"
and in Game.h I have made a class called Game and a enum called GAMESTATE.
In Graphics.h I have made a class called Graphics.
All the .cpp files for the class only include their class header and WndProc includes Main.h
and after all that I get this when I compile.
1>Graphics.obj : error LNK2005: "class Graphics TheGraphics" (?TheGraphics##3VGraphics##A) already defined in Game.obj
1>Graphics.obj : error LNK2005: "class Game TheGame" (?TheGame##3VGame##A) already defined in Game.obj
1>Main.obj : error LNK2005: "class Graphics TheGraphics" (?TheGraphics##3VGraphics##A) already defined in Game.obj
1>Main.obj : error LNK2005: "class Game TheGame" (?TheGame##3VGame##A) already defined in Game.obj
1>WndProc.obj : error LNK2005: "class Graphics TheGraphics" (?TheGraphics##3VGraphics##A) already defined in Game.obj
1>WndProc.obj : error LNK2005: "class Game TheGame" (?TheGame##3VGame##A) already defined in Game.obj
1>F:\Games\Zombie Lypse\Release\Zombie Lypse.exe : fatal error LNK1169: one or more multiply defined symbols found
please help I've looked everywhere and I've tried to fix it myself. Still nothing.
You have externed the globals everywhere but not defined it anywhere.
extern doesn't define variables. It just tells the compiler that the variable is defined elsewhere. So you have to actually define it elsewhere.
You can do this.
In the header file
/* main.h */
MYEXTERN Game TheGame;
MYEXTERN Graphics TheGraphics
In the first .c file
In main.cpp
/* MYEXTERN doesn't evaluate to anything, so var gets defined here */
#define MYEXTERN
#include "main.h"
In the other .cpp files
/* MYEXTERN evaluates to extern, so var gets externed in all other CPP files */
#define MYEXTERN extern
#include "main.h"
So it gets defined in only one .cpp file & gets externed in all the others.
I am working with form application in VC++. I have main form i.e. Form1.h and also the child form named child.h. I am calling the child.h form on the button click of the form1.h. For calling the child.h I have to include Child.h in Form1.h.
I have used the following code
in Form1.h
#incude "Child.h"
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
Child^ c=gcnew Child;
c->Visible=true;
}
And In Child.h I am doing some processing. For this I have made one header file named param.h having some function name and global variables name. I have included param.h in Child.h file.
And param.h is
#ifndef param_h_seen
#define param_h_seen
#define LED_Line 4
#define CALIBRATION_MODE 0
typedef unsigned __int32 uint32_t;
typedef unsigned __int8 uint8_t;
/****for LED ROI entered by user***/
int x_of_roi=6;
int y_of_roi=10;
/********************************/
/*************for graph ROI*******/
int ROIwidth=16;
int ROIheight=4096;
/********************************/
int LED_num= 64;
unsigned short *calib_factor;
/*********functions*****************/
int find_area(unsigned char *intensity,int start);
void DetectRectangle();
/***************************************/
#endif
After Including the child.h It is showing the error
PUMA_LED_TESTER.obj : error LNK2005: "unsigned short * calib_factor" (?calib_factor##3PAGA) already defined in Child.obj
PUMA_LED_TESTER.obj : error LNK2005: "int x_of_roi" (?x_of_roi##3HA) already defined in Child.obj
PUMA_LED_TESTER.obj : error LNK2005: "int y_of_roi" (?y_of_roi##3HA) already defined in Child.obj
PUMA_LED_TESTER.obj : error LNK2005: "int ROIwidth" (?ROIwidth##3HA) already defined in Child.obj
PUMA_LED_TESTER.obj : error LNK2005: "int ROIheight" (?ROIheight##3HA) already defined in Child.obj
PUMA_LED_TESTER.obj : error LNK2005: "int LED_num" (?LED_num##3HA) already defined in Child.obj
I don't know why these errors are coming.Can any body please tell me the solution to solve these errors
Thanks in Advance
int x_of_roi=6;
int y_of_roi=10;
Those are definitions, and should not be in your header files. Place them in one of the cpp files, and on the header have:
extern int x_of_roi
extern int y_of_roi;
Same goes with the rest of the global variables you declare in your header files. When those headers are included by more than one cpp file (namely translation unit), each unit effectively declares new variables with the same name, which the linker complains about.
Each time you #include a header into you source file, the result is the same as just copy/pasting the header's text. So, if you have a header which defines something:
header.h:
int magic = 0xA0B1C2D3
And you include it into multiply cpp files:
source1.cpp:
#include "header.h"
<...>
source2.cpp:
#include "header.h"
<...>
The result is that the variables and macros are being defined for each cpp. In this case, it is ok. But if you have more complicated dependencies, this may result in errors that you are currently getting.
In your case you are basically including the same file twice, and that leads to multiply definitions of stuff with the same name. What you need to do is to place the definitions outside of header files, and use extern when you need to access them from elsewhere.
I am trying to use a global variable from separated .cpp files. I have got an init.h file as:
//init.h
#ifndef init
#define init
int a = 3;
#endif
I have got an init.cpp file as:
//init.cpp
#include init.h
Then finally my main.cpp file is:
//main.cpp
#include "init.h"
int main(void)
{
while(1)
{
}
}
After this, I get the error:
1>init.obj : error LNK2005: "int a" (?a##3HA) already defined in main.obj
1> ..deneme.exe : fatal error LNK1169: one or more multiply defined symbols found
Why my #infdef control does not solve this problem?. I also tried using #pragma once but I got same error. What is wrong with my code?
You need to mark your variable as extern and define it only once in an implementation file.
As the code is now, you're breaking the one definition rule. The include guards don't help in this case, since all translation units that include that header re-define the variable.
What you actually need:
//init.h
#ifndef init
#define init
extern int a;
#endif
and the definition:
//init.cpp
#include "init.h"
int a = 3;
Also, think twice before using globals. What is it that you're actually trying to achieve?
I have a test class declared in a header file and defined in a separate file. The class must compile differently under Windows, so i use #if defined ( _WINDOWS_ ). When I compile the cpp file which also contains #if defined ( _WINDOWS_ ), the file is compiled as if the symbol _WINDOWS_ was not defined, although it is defined in another file. When I compile the code, I am getting the following error:
Error Code : error lnk2019 unresolved external symbol public
source code
// test.h
class Test
{
public:
#if defined (_WINDOWS_)
void printwindow();
#endif
void notwindows();
};
//test.cpp
#include "test.h"
#if defined (_WINDOWS_)
void Test::printwindow()
{
cout << "i am windows ";
}
#endif
void test::notwindows()
{
cout << " not windows " ;
}
//main.cpp
#include "windows.h"
#include "test.h"
void main()
{
test t1 ;
t1.printwindow() // OK I have declared function so my _WINDOWS_ is available but when i run it i get
}
Error Code : error lnk2019 unresolved external symbol public
NOTE : if i define the function directly it works without any problem
// test.h
class Test
{
public:
#if defined (_WINDOWS_)
void printwindow(){couT << "i am window" }
#endif
void notwindows();
};
but I don't like this method. I prefer to define them in separate files (h and cpp).
It would be better for you to use _WIN32 instead of _WINDOWS_ for your conditional compilation test. _WINDOWS_ is defined only if windows.h has been included, while the compiler automatically defines _WIN32 for any build for a Windows target, regardless of what headers are included. In your situation, _WINDOWS_ is defined when you compile main.cpp but not when you compile test.cpp becuase test.cpp doesn't include windows.h.
Also, the _WINDOWS_ macro definition is an implementation detail of the windows.h header, and is not guaranteed to be used. For example, the MinGW version of windows.h does not define _WINDOWS_.
You need to include windows.h in your test.cpp file
When you compile test.cpp it does not have the _WINDOWS_ symbol defined. So it creates an object file without the windows function.
Then in test.hpp it uses the symbol because you included windows.h in main.cpp
If you change the order of the includes it will behave differentially. You should rethink the way how you want to select the windows version which does not depend on the order of includes (for example see the other answer about _WIN32_).
You can not change the code in an already compiled object file (test.cpp) using some define in another code file (main.cpp)