Redefinitions With Include Guards Too

Hey agian, Im working on a sfml project and I finnally decided to stop having huge, single file programs. So I'm using multiple .cpp files and .h's too. I have the header Guard concept down except now I have a some errors which don't make any sense.
When I have a sepetate .cpp including a function I'm using in my main file, do I include the .cpp in main or in a header included in main. I'm getting two different errors :
error LNK2005: "int __cdecl ChooseMessage(void)" (?TheFunctionInTheotherCPP@@YAHXZ) already defined in Main.obj and
fatal error LNK1169: one or more multiply defined symbols found.

I checked to make sure all the headers had guards and the cpp's too. Heres the relevent source:
Main.cpp
1
2
3
4
5
6
7
8
9
#include "stdafx.h"
#include "Window.h"
//or should I directly include Window.cpp???
int main()
{
	StartWindow();
    return 0;
}

Window.h
1
2
3
4
5
6
7
8
#ifndef WINDOW_H 
#define WINDOW_H 

#include "Window.cpp"
 
// some useful definitions here

#endif 

Window.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "stdafx.h"

#ifndef WINDOW_CPP
#define WINDOW_CPP

#include "SFML/Window.hpp"
#include "SFML/Graphics.hpp"

int ChooseMessage()
{
  //Pick random Title
	return EXIT_SUCCESS;
}

int StartWindow(){
    std::string WindowMessage;
    sf::VideoMode Resolution(320,240);
    sf::RenderWindow window(Resolution,WindowMessage);
    //Unfinished - This just creates an sfml window with a different title
    ChooseMessage();
	return EXIT_SUCCESS;	
}
#endif 


I got an error message for both StartWindow() and ChooseMessage()
I searched but nothing relevent appeared.
Thanks in advance :)
Last edited on
you may put declaration of the function ChooseMessage and StarWindow in window.h,instead of incuding 'window.cpp'
have a try....

Window.h:
1
2
3
4
5
6
7
#ifndef WINDOW_H 
#define WINDOW_H 

int ChooseMessage() ;
int StartWindow() ;

#endif  


Window.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
include "stdafx.h"
#include "SFML/Window.hpp"
#include "SFML/Graphics.hpp"
#include "Window.h"

int ChooseMessage()
{
  //Pick random Title
	return EXIT_SUCCESS;
}

int StartWindow(){
    std::string WindowMessage;
    sf::VideoMode Resolution(320,240);
    sf::RenderWindow window(Resolution,WindowMessage);
    //Unfinished - This just creates an sfml window with a different title
    ChooseMessage();
	return EXIT_SUCCESS;	
}
Last edited on
When I use my IDE (KDevelop or QtCreator) I use the class wizard to create new classes. The IDE creates the . h & .cpp files automatically and puts in the header guards too. When I create a new function in the header file, it makes a stub in the .cpp file also.

So my point is, your IDE should do a lot of stuff for you, so you shouldn't have worry whether things are in the right place.
You should never #include .cpp files. In the semi-rare occasions when a header needs to define functions and you want to put them in a separate file, it's common to use a .inl (for inline) file and include it in just that one header, and nowhere else. The extension is chosen because there's no chance of someone mistakenly thinking the file should be compiled.

When I use my IDE (KDevelop or QtCreator) I use the class wizard
Real Men don't believe in magic.
Real Men don't believe in magic.


Well at least it will give the OP an idea of what it should look like, to start with.
Well at least it will give the OP an idea of what it should look like, to start with.


Discussion

If the OP doesn't know what a program containing two cpp files and an h file should look like, he should dump the IDE and do it by hand until he does know what it looks like. In the case above, the OP clearly does not know what it should look like and as such has managed to have the exact same function defined in two separate cpp files, causing this link error.

He also clearly does not understand the process by which source code compiles to object code links to binary executables/libraries; if he did, he'd know how to fix this link error. Again, this is only possible because people use IDEs without understanding what they're doing.


Explanation of problem

In this case, the compiler sees two cpp files to compile. One called window.cpp, which contains function int ChooseMessage(). This cpp file is compiled into a binary object.

Then, COMPLETELY SEPARATELY (because that's how it works) the cpp file Main.cpp is compiled, and it contains function int ChooseMessage(). Include guards are irrelevant here. When it gets to them, it checks to see if it has already included the file IN THIS CPP FILE. There is no knowledge of what happened in the other cpp file, which is entirely correct and the right thing to do. This cpp file is compiled into a binary object.

Then, the linker tries to join together these two binary objects. It lokos through them for the function int ChooseMessage() and finds it TWICE! It does not know which one you mean to use, so it throws an error and halts.
Last edited on
OK, so the OP doesn't know what it should look like, or how it works - I agree he should be educated about that.

I was only putting forward the idea that using the IDE properly might aid that education a little.

Because the OP doesn't know - I suspect he has just typed code in as he saw fit, without making any use of the functionality of the IDE. If he had used the class wizard, and the ability to make stub functions in the .cpp (or .hpp )file, then he might have possibly had a better outcome.

It is best to learn things properly, rather than blindly using tools, but sometimes technology might help a bit.

I guess the problem is that the OP hasn't learnt how to use that technology properly either.
Ok, Thanks for the extensive help on that. I got it to work now, I never used multiple files and this first project I tried with them I have about 15 files and 200 line total, which is way too spreadout I think.
200 lines total over 15 files...I'm guessing you're setting up the framework first and then adding in all the actual code later? If so, just be sure to remember where you need to implement all your dummy functions. You can put #error lines inside the dummy functions so you don't accidentally compile them until they're done.
I have about 15 files and 200 line total, which is way too spreadout I think.


Normally you would have a header & .cpp file for each class. Provided you have designed the classes properly, then the number of files shouldn't matter.

Seen as this is the first time you have tried compiling different files, then you should be aware that class design is a separate skill - it can add a lot of complexity for how they interoperate and can be tricky to get right.

Unless you are not using classes, in which case you should group related functions into a file. If that is the case then 15 files does sound too much.

Maybe you could give us an outline of your project.
Your Right L B, I'm just laying out the framework so far. I am making a SFML sidescroller somewhat like terraria, and I make two simple functions and this happened.

The outline is like:
1
2
//Main.cpp//
Completely compose of simple or user defined functions

Main.cpp
Each section is broken down like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
GameMain.cpp
#include{
-Player.cpp
  #include
    -Health.cpp // Contains Damage, Falling, Armor
    -Info.cpp    // Everything include things like XP
    -Inventory.cpp
         -Pouch 1          //Not cpp's
         -Pouch 2
         -Pouch 3
         -Pouch 4
         -Pouch misc
-World.cpp
-Mobs.cpp
-Items.cpp
}
Thats not good, of course now that I compile it it gives me an error...

function 'int StartWindow(void)' already has a body
see previous definition of 'StartWindow'


Window.h:
1
2
3
4
5
6
7
#ifndef WINDOW_H 
#define WINDOW_H 

int ChooseMessage() ;
int StartWindow() ;

#endif   

Window.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
include "stdafx.h"
#include "SFML/Window.hpp"
#include "SFML/Graphics.hpp"
#include "Window.h"

int ChooseMessage()
{
  //Pick random Title
	return EXIT_SUCCESS;
}

int StartWindow(){
    std::string WindowMessage;
    sf::VideoMode Resolution(320,240);
    sf::RenderWindow window(Resolution,WindowMessage);
    //Unfinished - This just creates an sfml window with a different title
    ChooseMessage();
	return EXIT_SUCCESS;	
}



And the function is also defined in the .cpp with
int ChooseMessage(){//code}

I know its defined twice but I tried deleting and adding all sorts of combinations but I still gives me the errors.
Last edited on
It isn't defined twice in this code. It is declared in Window.h and defined in Window.cpp.

As long as you're only including the header file (and not redefining the function somewhere,) there shouldn't be any violation of the one definition rule.
Wow, I was including the .cpp in the .h Thanks for the help
Topic archived. No new replies allowed.