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
Related
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.
I have an MFC application AVT_testapp, and in the header file (AVT_testappDlg.h) I am trying to create a variable outside of all functions, classes, etc. in order to make it global. Whenever I try to do this though (say I try int x = 7), I get the error:
1>AVT_testappDlg.obj : error LNK2005: "int x" (?x##3HA) already defined in
AVT_testapp.obj
1>..\..\bin\x64\Debug\AVT_testapp.exe : fatal error LNK1169: one or more
multiply defined symbols found
Everything I have found on google says "just add header guards". AVT_testappDlg has 6 #include's, and each of them has header guards.
What else could be causing these errors when creating global variables?
EDIT: Here is the beginning of my header file,
#pragma once
#include "../../src/CoreUtils/nierr.h"
#include "..\..\src\CoreUtils\StringHelpers.h" //includes windows.h
#include "afxwin.h"
#include "afxcmn.h"
#include "IFrameObserver.h"
#include "c:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include\GdiPlusHeaders.h"
//#include <fstream>
//#include <windows.h>
int x = 7;
using namespace AVT::VmbAPI;
//////////////////////////////////////////////////////////////////////////
////////// MyObserver class ///////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
class MyObserver : public IFrameObserver
{
private:
MyObserver( MyObserver& );
MyObserver& operator=( const MyObserver& );
public:
VmbUchar_t* imageData;
//...
//...
//...
//...
//that's the end of the relevant stuff
You cannot define variables at namespace level in a header. In general it is best not to have global variables, but if you need to you should provide only a declaration in the header and the definition in a single .cpp:
//header
extern int i;
//cpp
int i;
The problem with your code is not related to header guards. Header guards ensure that a header is parsed only once in each translation unit. Lack of header guards causes compiler errors, where the compiler sees, say for example a class, defined multiple times in the same translation unit after preprocessing. In your case the error is a linker error LNK2005, and it means that the same symbol was defined in multiple translation units (in your case in each translation unit that includes the header with the definition).
If the global variable is not const(*), you cannot put it in a header file and include it in multiple translation units (i.e. .cpp files). Otherwise, you will end up with multiple definitions of the same symbol in your program, violating the ODR (One Definition Rule, see Paragraph 3.2 of the C++11 Standard), and the linker will complain about that.
You should use the extern modifier in your shared header to provide only a declaration of your variable:
extern int var;
Then, in one single .cpp file, you can provide a definition for it:
int var;
(*) const global variables have internal linkage by default, so each translation unit will end up having a private copy of it and no multiple definition will occur.
if you insist on having a global variable at least put it in a namespace to avoid collisions with other modules
namespace globals
{
extern int x;
}
then in the .cpp file define it.
int globals::x = 0;
it also makes it more clear that it is a global variable.
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?
getting this
1>main_display.obj : error LNK2005: "struct ALLEGRO_DISPLAY * main_display" (?main_display##3PAUALLEGRO_DISPLAY##A) already defined in event_queue.obj
1>main.obj : error LNK2005: "struct ALLEGRO_DISPLAY * main_display" (?main_display##3PAUALLEGRO_DISPLAY##A) already defined in event_queue.obj
1>main.obj : error LNK2005: "struct ALLEGRO_TIMER * timer" (?timer##3PAUALLEGRO_TIMER##A) already defined in event_queue.obj
1>main.obj : error LNK2005: "struct ALLEGRO_EVENT_QUEUE * event_queue" (?event_queue##3PAUALLEGRO_EVENT_QUEUE##A) already defined in event_queue.obj
1>main_timer.obj : error LNK2005: "struct ALLEGRO_TIMER * timer" (?timer##3PAUALLEGRO_TIMER##A) already defined in event_queue.obj
Any Idea what can cause this?
EDIT:
main_display.h:
#pragma once
#include <allegro5/allegro.h>
#include <stdio.h>
#define SCREEN_W 640
#define SCREEN_H 480
extern ALLEGRO_DISPLAY *main_display = NULL;
void display_init();
void destroy_display();
event_queue.h
#pragma once
#include <stdio.h>
#include <allegro5/allegro.h>
#include "main_timer.h"
#include "main_display.h"
extern ALLEGRO_EVENT_QUEUE *event_queue = NULL;
void event_queue_init();
void event_queue_destroy();
Looks like those structures are defined in a header file. Then they're #included into multiple translation units. You need to make it such that there's only one definition of the particular item.
Given that these are global variables, the way you generally do that is declaring them by marking them extern in the header, and then defining them in some translation unit.
I'm guessing you put function implementations into a header (.h) file without using an inline declaration.
As the header file is included in multiple sources, the body of the function gets compiled multiple times. The linker is complaining about seeing the function more than once.
It looks like you're defining the same struct in different files.
Without actually seeing the files, that's about as far as I get...