Same header file in 2 source files problem

Aug 24, 2011 at 4:18pm
My compiler gives me a warning for having the same header file in 2 source files. I don't know how else I would make my application.Here is my shorten code:
mapmaker.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef MAPMAKER
#define MAPMAKER
#include "include.h"
using namespace std;

struct map_maker
{
	vector<map> maps;
	unsigned short int keyboard[256];
	map_maker();
};

#endif 

winmanager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef _WINMANAGER
#define _WINMANAGER
#include "resource.h"
#include "include.h"
#include "mapmaker.h"

class winManager
{
	map_maker* process;
	winManager();
};
winManager::winManager()
{
	process=new map_maker;
}
extern winManager window;

#endif 

winmanager.cpp
1
2
3
#include "winManager.h"
#include "winFunctions.h"
using namespace std;

main.cpp
1
2
3
#include "include.h"
#include "winManager.h"
using namespace std;
Aug 24, 2011 at 4:26pm
what does the head of "include.h" look like?
Aug 24, 2011 at 4:32pm
Doesn't matter, I get the error because of the map_maker struct being defined in two .cpp files.
Aug 24, 2011 at 5:00pm
I get the error because of the map_maker struct being defined in two .cpp files.


No you don't. Doing that is perfectly legal.

What exactly is the actual error?
Aug 24, 2011 at 5:08pm
My error:
1>winManager.obj : error LNK2005: "public: __thiscall winManager::winManager(void)" (??0winManager@@QAE@XZ) already defined in main.obj
1>winManager.obj : error LNK2005: "public: __thiscall map_maker::map_maker(void)" (??0map_maker@@QAE@XZ) already defined in main.obj
1>winManager.obj : error LNK2005: "public: __thiscall map::map(void)" (??0map@@QAE@XZ) already defined in main.obj
1>winManager.obj : error LNK2005: "public: __thiscall object::object(void)" (??0object@@QAE@XZ) already defined in main.obj
Map Maker.exe : fatal error LNK1169: one or more multiply defined symbols found

Map and Object are defined in mapmaker.h along with map_maker.

My include.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#ifndef _INCLUDE
#define _INCLUDE
#include <windows.h>
#include <commctrl.h>
#include <glew.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include "wglew.h"
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <vector>
#include <string>
#include <sstream>
#include <glm/glm.hpp>
#include <glm/ext.hpp>
#define WIN32_LEAN_AND_MEAN
#pragma comment(linker,"/manifestdependency:\"" \
    "type='win32' " \
    "name='Microsoft.Windows.Common-Controls' " \
    "version='6.0.0.0' " \
    "processorArchitecture='*' "  \
    "publicKeyToken='6595b64144ccf1df' " \
    "language='*'\"")
#pragma comment(lib,"comctl32")
#pragma comment(lib,"opengl32")
#pragma comment(lib,"glew32.lib")
#endif 
Aug 24, 2011 at 5:13pm
Your problem is because you are putting non-inline function bodies in your headers. Take a look at Winmanager.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef _WINMANAGER
#define _WINMANAGER
#include "resource.h"
#include "include.h"
#include "mapmaker.h"

class winManager
{
	map_maker* process;
	winManager();
};
winManager::winManager()   // <- defined in the header
{
	process=new map_maker;
}
extern winManager window;

#endif  


This will create duplicate function bodies which will confuse the linker. Solutions are:

1) Implicitly inline it (put the function body in the actual class
or
2) Explictly inline it (put the 'inline' keyword at the start of line 12)
or
3) Move the function body to a cpp file.

You also have the same problem with your map_maker, map, and object constructors.
Aug 24, 2011 at 5:40pm
Note that if you use

#define WIN32_LEAN_AND_MEAN

it must be before windows.h, or you're wasting your time defining it. It's purpose is to tell windows.h to only include the most commonly used headers, to speed up compile time.
Topic archived. No new replies allowed.