Linking and Includes

Hi !

I have read some guides to understand what should I put in source files(cpp/c) and what in header files(h) (like http://www.cplusplus.com/forum/articles/10627/).

Some things I got from this guides is I should declare the functions headers in headers, to guard them, to don't include source files, etc.

So, I tried to do something like this:

emeralds.cpp
1
2
3
4
5
6
7
8
#include "emeralds.h"

int _tmain(int argc, _TCHAR* argv[])
{
	readConfig();

	return 0;
}


emeralds.h
1
2
3
4
5
6
7
#ifndef _EMERALDS_

#define _EMERALDS_

#include "configReader.h"

#endif 


configReader.cpp
1
2
3
4
5
6
#include "configReader.h"

bool readConfig()
{
//some function
}


configReader.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef _CONFIG_READER_

#define _CONFIG_READER_

#include <SDKDDKVer.h>
#include <tchar.h>

struct config
{
//some struct
} serverConfig;

bool readConfig();

#endif 


I tried to compile this in VS2013. I disabled precompiled header(shouldn't I ?).

I got these errors:
1
2
3
4
Error	1	error LNK2005: "struct config serverConfig" (?serverConfig@@3Uconfig@@A) already defined in configReader.obj	D:\Info\cvis\emeralds\emeralds\emeralds.obj	emeralds
Error	2	error LNK1169: one or more multiply defined symbols found	D:\Info\cvis\emeralds\Debug\emeralds.exe	1	1	emeralds



I searched and I found that means I defined some things twice, but I don't know where .. :( How can I solve these problems ?

Am I right about what should I write in headers and source files ?
on your header file i would remove serverConfig.

just have a struct as

1
2
3
4
struct config
{
//some struct
};


if you want to use the struct just instantiate it.

 
config serverConfig;
Last edited on
Topic archived. No new replies allowed.