Problem with header inclusions
Feb 7, 2010 at 2:41pm UTC
Hi,
When I include the same header file of my own project in more than one code file (.cpp), it gives me a linking error (LNK2005) which says that the function of the header file is already defined.
This is my header file:
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#ifndef CREATE_DIRECTORY_H
#define CREATE_DIRECTORY_H
#include "OSDefine.h"
#include <string>
#include "Exception.h"
using namespace std;
namespace PEARL
{
#ifdef OS_WINDOWS
#include <direct.h>
void CreateDirectory(string path)
{
if (_mkdir(path.c_str()) == -1)
{
string sErrorMessage = "Could not create the directory \"" ;
sErrorMessage += path;
sErrorMessage += "\" " ;
if (errno == 17)
sErrorMessage += "because a file, directory or device already exists with the same path" ;
else if (errno == 2)
sErrorMessage += "because the path can not be found" ;
else
sErrorMessage += "for an unknown reason" ;
throw new Exception(errno, sErrorMessage);
}
}
#endif
bool CreateDirectoryIfExists(string path)
{
bool created = true ;
try
{
CreateDirectory(path);
}
catch (Exception *ex)
{
created = false ;
if (ex->ErrorCode != 17)
throw ex;
}
return created;
}
}
#endif
And this are my errors:
Error 6 error LNK2005: "void __cdecl PEARL::CreateDirectory(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?CreateDirectory@PEARL@@YAXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Investigation.obj TextBased.obj
Error 7 error LNK2005: "bool __cdecl PEARL::CreateDirectoryIfExists(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?CreateDirectoryIfExists@PEARL@@YA_NV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Investigation.obj TextBased.obj
Error 8 error LNK2005: "void __cdecl PEARL::CreateDirectory(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?CreateDirectory@PEARL@@$$FYAXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Investigation.obj TextBased.obj
Error 9 error LNK2005: "bool __cdecl PEARL::CreateDirectoryIfExists(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?CreateDirectoryIfExists@PEARL@@$$FYA_NV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Investigation.obj TextBased.obj
Error 12 fatal error LNK1169: one or more multiply defined symbols found C:\Users\Magnificence\Documents\Visual Studio 2008\Projects\PEARL\Debug\PEARL.exe
Last edited on Feb 7, 2010 at 2:42pm UTC
Feb 7, 2010 at 2:54pm UTC
Don't define functions in headers. Put only declarations in headers and the definitions in source files.
Feb 7, 2010 at 3:05pm UTC
a thanks!
Topic archived. No new replies allowed.