I have no idea what is this(C++ linker error) - c++

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...

Related

Re-definitions of functions while including files in C++ (error LNK2005)

I'm new to C++ and I have a basic doubt.
I'm creating a french verb conjugating application.
I have two files, a Conjugator.cpp file and an ErVerbs.cpp file.
I want to keep the bulk of my functions in the ErVerbs source file and use the conjugator
file to use these functions.
Here are a few code snippets:
Conjugator.cpp
#include <iostream>
#include <string>
#include "Variables.h"
#include "ErVerbs.cpp"
#include "IrVerbs.cpp"
#include "ReVerbs.cpp"
using namespace std;
void check()
{
if (verb.substr(len - 2, len) == "er")
erVerbs();
else if (verb.substr(len - 2, len) == "ir")
irVerbs();
else if (verb.substr(len - 2, len) == "re")
reVerbs();
}
int main()
{
cout << "Enter verb : ";
getline(cin, verb);
cout << "Enter tense : ";
getline(cin, tense);
len = verb.length();
check();
}
ErVerbs.cpp
#include <iostream>
#include <string>
using namespace std;
void erVerbs()
{
cout << "er Verb"; cin.get();
}
Similarly, I have three other such .cpp source files with similar functions.
When I build the program, I get an error that each of the methods I'm using has been defined
already.
1>ErVerbs.obj : error LNK2005: "void __cdecl erVerbs(void)" (?erVerbs##YAXXZ) already defined in Conjugator.obj
1>ErVerbs.obj : error LNK2005: "void __cdecl erVerbs(void)" (?erVerbs##$$FYAXXZ) already defined in Conjugator.obj
1>IrVerbs.obj : error LNK2005: "void __cdecl irVerbs(void)" (?irVerbs##YAXXZ) already defined in Conjugator.obj
1>IrVerbs.obj : error LNK2005: "void __cdecl irVerbs(void)" (?irVerbs##$$FYAXXZ) already defined in Conjugator.obj
1>ReVerbs.obj : error LNK2005: "void __cdecl reVerbs(void)" (?reVerbs##YAXXZ) already defined in Conjugator.obj
1>ReVerbs.obj : error LNK2005: "void __cdecl reVerbs(void)" (?reVerbs##$$FYAXXZ) already defined in Conjugator.obj
I'd be extremely grateful if someone could tell me how to save functions in separate source files and #include them in one source files and use their functions without re-definition errors.
Dont:
#include "ErVerbs.cpp"
in Conjugator.cpp, this is what causing your linker errors. By including your cpp files like that you redefine this function again.
You should create ErVerbs.h file and put in it declaration for your function:
#if !defined(ER_VERBS_H)
#define(ER_VERBS_H)
void erVerbs();
#endif
and in Conjugator.cpp, include #include "ErVerbs.h", and the same for other your functions.
You should never include *.cpp files. Delete following
#include "ErVerbs.cpp"
#include "IrVerbs.cpp"
#include "ReVerbs.cpp"
Create erVerbs.h with following content:
void erVerbs();
and include it in Conjugator.cpp
#include "ErVerbs.h"
As you included modules
#include "ErVerbs.cpp"
#include "IrVerbs.cpp"
#include "ReVerbs.cpp"
in module Conjugator.cpp then all four modules contain definitions of the functions and the compiler says about this.
You have to declare the functions in some header file and include it in all modules that uses these functions while their definitions to keep in one module (or among several modules) that will not be included in any other module.
You're #including a .cpp file from another .cpp file, so the function definition will exist in two places.
What you probably want to do is create a Conjugator.h header file, declaring (but not defining) the function, and include that instead.
Also look up header guards (or #pragma once) while you're at it, if you're not aware of how to prevent multiple declarations in .h files.

Trouble with #include, C++

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

Combine C++ and C

I was trying to do a DirectX tutorial, but I wanted to write the application in C and not in C++. So I wrote the code in C, but when I tried to compile it I got lot's of errors on the "setupD3D" function. So I just renamed a file to .cpp. But the new code didn't compile either. I think that combining C and C++ is the problem, or there are to many cross-references. Can anyone tell me what the problem is in this code?
direct3d.h:
#include "main.h"
#ifndef DIRECT3D
#define DIRECT3D
int m_videoCardMemory;
char m_videoCardDescription[128];
ID3D11DeviceContext* m_deviceContext = 0;
D3DXMATRIX m_projectionMatrix;
D3DXMATRIX m_worldMatrix;
D3DXMATRIX m_orthoMatrix;
int setupD3D(BYTE vsync, HWND hwnd, float screenDepth, float screenNear);
void terminateD3D();
void beginScene(float red, float green, float blue, float alpha);
void endScene();
#endif
direct3d.cpp
#include "direct3d.h"
// code
main.h:
#ifndef MAIN_FUNC
#define MAIN_FUNC
#pragma comment(lib, "dxgi.lib")
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "d3dx11.lib")
#pragma comment(lib, "d3dx10.lib")
#include <Windows.h>
#include <dxgi.h>
#include <d3dcommon.h>
#include <d3d11.h>
#include <d3dx10math.h>
#include "direct3d.h"
int breedte, hoogte;
LRESULT CALLBACK Actie(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
void main_loop(MSG* msg);
void keyevent();
#endif
main.c
#include "main.h"
// code
winstart.c
#include "main.h"
// code
Errors:
Error 86 error LNK2005: _m_deviceContext already defined in main.obj
Error 87 error LNK2019: unresolved external symbol _setupD3D referenced in function _Actie#16
Error 88 error LNK2019: unresolved external symbol _terminateD3D referenced in function _Actie#16
Error 89 error LNK2019: unresolved external symbol _beginScene referenced in function _render
Error 90 error LNK2019: unresolved external symbol _endScene referenced in function _render
Error 91 error LNK1120: 4 unresolved externals
I also have 84 warnings about macro redefinitions.
If you declare functions in a .cpp file the compiler will compile it as C++ code (no matter if it's pure C or not). Unfortunately C++ has a mechanism called Name mangling which allows for example overloading of functions while C doesn't. If you now want to call such a function fro a .c file (which is recognized as pure C by the compiler) the compiler is creating references for another naming of the symbols (functions in your case) than they exist and the linker is not able to match them anymore.
To solve this, you can put an extern "C" in front of every function you want to be callable by C. As C does not recognize extern "C" you have to make this language dependent.
A common way to solve this problem is to bring follwing structure in the header file:
... your include guard of choice
#ifdef __cplusplus
extern "C" {
#endif
//-- your declarations
#ifdef __cplusplus
}
#endif
This instructs the C++ compiler to create names that are C compliant. As the C compiler does not provide the preprocessor symbol "__cplusplus" it ignores the extern statement. (Which is ok as the C compiler always only creates C compliant symbol names)
Instead of changing header files (sometimes you are not allowed to) it's also legal to include a whole header file embraced by extern "C":
extern "C" {
#include <My_C_API.h>
}
You have two types of link errors :
Error 86 error LNK2005: _m_deviceContext already defined in main.obj
is because you define m_deviceContext in a header file, and then proceed to include that header file in multiple compilation units (at least in direct3d.cpp, main.c and winstart.c). Move definitions out of header files, and also take care of your include dependencies (main.h and direct3d.h depend on each other - that seems odd).
Error 87 error LNK2019: unresolved external symbol _setupD3D referenced in function _Actie#16
Error 88 error LNK2019: unresolved external symbol _terminateD3D referenced in function _Actie#16
Error 89 error LNK2019: unresolved external symbol _beginScene referenced in function _render
Error 90 error LNK2019: unresolved external symbol _endScene referenced in function _render
Error 91 error LNK1120: 4 unresolved externals
is because you're mixing C and C++ code incorrectly. Either compile everything with a C compiler (or a C++ compiler), or make correct use of extern "C". For more details, check eg. the relevant section of the C++ FAQ Lite.
The error says that something is already defined in main.o.
What you do is put the include before the guards, so no matter what it will include that file possibly creating duplicate code. And you have circular dependencies.
In the file direct3d.h:
#include "main.h"
#ifndef DIRECT3D
#define DIRECT3D
it should be:
#ifndef DIRECT3D
#define DIRECT3D
#include "main.h"
The include in main.c:
#include "main.h"
is a problem because you already include it in direct3d.h which is including main.h.
So the compiler probably goes like:
#include "main.h"
#include "direct3d.h"
#include "main.h" <-- duplicate code detected
throw error.
And there is no problem in mixing C and C++ together.

VC++ Error LNK2005 Already Defined In

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.

one or more multiply defined symbols found error in winform vc++ application

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.