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?
Related
I have declared a global variable in header.h and included that header in source.cpp and main.cpp but linker is giving error
Source.obj : error LNK2005: "int globalVariable" (?globalVariable##3HA) already defined in Main.obj
GlobalVariableAndLinkageIssue.exe fatal error LNK1169: one or more multiply defined symbols found
header.h
int globalVariable;
source.cpp
#include "header.h"
main.cpp
#include"header.h"
void main() {}
Move the declaration to a .cpp file. You can use a declaration in a header file by using:
extern int globalVariable; // declaration in header - can have as many as you need
But the .cpp file should have the definition:
int globalVariable; // definition in .cpp - only need one across all your files
C and C++ use textual pre-processor to include headers, this is basically a text insertion, not a smart module system as in some languages. By including it as you were, you are creating multiple definitions, one per .cpp file.
As a matter of good practice, you need to get used to using include guards to protect against multiple nested includes (though it would not solve your current issue). If using Visual C++, you can use #pragma once or to use a portable solution wrap your header code in:
#ifndef _INCLUDE_FOO_H_
#endif
To create a global variable, you should do the following. Note that in the header, we mark the variable as extern, and we actually create the object in a cpp file.
header.h
extern int globalVariable;
header.cpp
#include "header.h"
int globalVariable;
main.cpp
#include "header.h"
int main() {}
Put global variable in some .c or .cpp file, so that it can be defined only once and refer in header file using extern
for example,
header.h
extern int globalVariable;
header.cpp
int globalVariable = 0;
source.cpp
#include "header.h"
main.cpp
#include"header.h"
int main() {
return 0;
}
Because BOTH sources #include your header, and thus it is DEFINED twice.
In such situation,it is common to use some #define as follows:
//header.h
#ifdef DEFINE_VARS
#define DEFINE_OR_DECLARE
#else
#define DEFINE_OR_DECLARE extern
#endif
DEFINE_OR_DECLARE int globalVariable;
//main.cpp
#define DEFINE_VARS
#include "header.h"
...
//header.cpp
#include "header.h"
...
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
I've defined a struct in a header file global.h, that i try to use it in a another class, but i get this error : Error 6 error LNK2001: unresolved external symbol "struct tag_KG_Data g_GlobalVar" (?g_GlobalVar##3Utag_KG_Data##A) KGComThread.obj
#ifndef GLOBAL_H_
#define GLOBAL_H_
#include <stdio.h>
typedef struct tag_KG_Data
{
int nKGStationID;
int nKGComPort;
}GLOBAL_VAR;
#endif
and in KGComThread.cpp file i use it like this:
#include "global.h"
extern GLOBAL_VAR g_GlobalVar;
I think the compiler can't find the global.h file so it defines a meaningless tag_KG_Data struct, but i can't understand why.
This
extern GLOBAL_VAR g_GlobalVar;
is only a declaration. The variable is not yet defined:
GLOBAL_VAR g_GlobalVar;
You need the previous line in a single implementation file.
Also, since this is C++, you don't need a tag for the struct, you can just write
struct GLOBAL_VAR
{
int nKGStationID;
int nKGComPort;
};
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 recently tried to create a global header file which would have all definitions of error codes (i.e. NO_ERROR, SDL_SCREEN_FLIP_ERROR, etc.) these would just be integers which I would define here.
I included these in both of my .cpp files, however I am getting an error where it is stated that I am defining then twice.
globals.h:
#pragma once
// error related globals
int SCREEN_LOAD_ERROR = 1;
int NO_ERROR = 0;
main.cpp:
#include "globals.h"
#include "cTile.h"
/* rest of the code */
cTile.h:
#pragma once
#include "globals.h"
class cTile {
};
It is complaining that SCREEN_LOAD_ERROR and NO_ERROR are defined twice, but as far as I know #pragma once should prevent this (I also tried #ifndef, but this also did not work).
compiler output:
1>main.obj : error LNK2005: "int SCREEN_LOAD_ERROR" (?SCREEN_LOAD_ERROR##3HA) already defined in cTile.obj
1>main.obj : error LNK2005: "int NO_ERROR" (?NO_ERROR##3HA) already defined in cTile.obj
Am I missing something?
Do not declare variables inside your header file.
When you declare a variable in header file a copy of the variable gets created in each translation unit where you include the header file.
Solution is:
Declare them extern inside one of your header file and define them in exactly one of your cpp file.
globals.h:
extern int SCREEN_LOAD_ERROR;
extern int NO_ERROR;
globals.cpp:
#include "globals.h"
int SCREEN_LOAD_ERROR = 0;
int NO_ERROR = 0;
main.cpp:
#include "globals.h"
cTile.h:
#include "globals.h"
You could simply use an enum:
globals.h:
enum
{
SCREEN_LOAD_ERROR = 1,
NO_ERROR = 0,
// ...
}
using #ifndef works fine.(Although it works, this is not best practice). try like this:
globals.h
#ifndef GLOBALS
#define GLOBALS
int SCREEN_LOAD_ERROR = 1;
int NO_ERROR = 0;
#endif
cTile.h:
#include "globals.h"
class cTile {
};
main.cpp:
#include "globals.h"
#include "cTile.h"
/* rest of the code */