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

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.

Related

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

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.

LNK2005: " already defined error

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 no idea what is this(C++ linker error)

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

Pesky Link Errors

I am using visual studio 2010 and believe I have a project settings issue. I have a header file that has some declarations in it:
definitions.h
#include <string>
struct myStruct
{
std::string x[4];
std::string y[8];
};
void InitializeStructData();
extern myStruct data[12];
and the cpp file initializes my structure:
definitions.cpp
#include "definitions.h"
#include <string>
mySturct data[12];
void InitializeStructData()
{
data[0].x[0] = "a";
data[0].x[1] = "b";
....
data[0].y[0] = "a";
....
....
data[11].y[7] = "done initializing"';
}
and I have a form that has some buttons and things whose text I populate from the arrays depending on different circumstances:
myForm.cpp
#include "definitions.h"
...
//form initialization
As soon as I have two #include "definitions.h" statements I get link errors:
Error 1 error LNK2005: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * Definitions"
Error 2 error LNK1169: one or more multiply defined symbols found
Your question is missing the important part.
You have a std::string* Definitions in a header, that you forgot to use extern with.
Do you have your code (.h file) inside:
#ifndef DEFINITIONS_H
#define DEFINITIONS_H
#endif
to help prevent you from defining it multiple times, if you have it included in multiple places?