Multiple function definition when using custom header files

Hello. I've recently been trying to take a giant mess of code that I'm working on and split it into small, more convenient files by adding various .h and .cpp files to a C::B project. I've read a little on header guards and preprocessor directives and understand what the multiple definition error is, but I'm having trouble actually finding where I've defined a function more than once. Anyway, I have 7 files, which I will detail below.

main.cpp, contains most of my code and makes use of code in all headers and .cpp files; has the following at the top
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
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string.h>
#include <fstream>
#include <iomanip>
#include <time.h>
#include <windows.h>
#include <wchar.h>


#ifndef CONCOL_H_INCLUDED
#include "concol.h"
#endif


#ifndef SOMEFUNCTIONS_H_INCLUDED
#include "somefunctions.h"
#endif

#ifndef ITEM_H_INCLUDED
#include "item.h"
#endif

using namespace std;


globals.cpp, contains a bunch of global variables that all files use; has this at the top
1
2
3
4
#include <cstdlib>
#include <iostream>
#include <string.h>
using namespace std;


concol.h, code that I found here: http://www.cplusplus.com/articles/2ywTURfi/ that allows me to change output color; it contains functions textcolor() and backcolor() and looks something like this:
1
2
3
4
5
6
7
8
9
10
#ifndef CONCOL_H_INCLUDED
#define CONCOL_H_INCLUDED
#ifndef _INC_WINDOWS
#include<windows.h>
#endif /*_INC_WINDOWS*/

//a few variable definitions
//prototypes for textcolor() and backcolor()
//definitions of textcolor() and backcolor()
#endif // CONCOL_H_INCLUDED 


item.h, a header for a class called "item"; looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
#include <cstdlib>
#include <iostream>
#include <string.h>

#ifndef ITEM_H_INCLUDED
#define ITEM_H_INCLUDED

using namespace std;

//declare the class and its members here

#endif // ITEM_H_INCLUDED 


item.cpp, contains the definitions of the member functions in class "item", has this at the top:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string.h>
#include <fstream>
#include <iomanip>
#include <time.h>
#include <windows.h>
#include <wchar.h>


#ifndef CONCOL_H_INCLUDED
#include "concol.h"
#endif

#ifndef SOMEFUNCTIONS_H_INCLUDED
#include "somefunctions.h"
#endif

#ifndef ITEM_H_INCLUDED
#include "item.h"
#endif

using namespace std;


somefunctions.h, a header containing the function prototypes for most of the functions used in other files, looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cstdlib>
#include <iostream>
#include <string.h>


#ifndef CONCOL_H_INCLUDED
#include "concol.h"
#endif


#ifndef ITEM_H_INCLUDED
#include "item.h"
#endif
using namespace std;

#ifndef SOMEFUNCTIONS_H_INCLUDED
#define SOMEFUNCTIONS_H_INCLUDED

//here are all of the prototypes
#endif // SOMEFUNCTIONS_H_INCLUDED 


somefunctions.cpp, contains the definitions of all the functions, has this at the top:
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
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string.h>
#include <fstream>
#include <iomanip>
#include <time.h>
#include <windows.h>
#include <wchar.h>


#ifndef CONCOL_H_INCLUDED
#include "concol.h"
#endif


#ifndef SOMEFUNCTIONS_H_INCLUDED
#include "somefunctions.h"
#endif

#ifndef ITEM_H_INCLUDED
#include "item.h"
#endif

using namespace std;


I tried to include the guards in the .cpp files so that no header would be included more than once, but when I attempt to compile this, I get the following error:

multiple definition of 'textcolor()'

Apparently this was defined in a function called Z9textcolorv (which I didn't create, but is instead included in a file called char_traits.h). The same thing happens for backcolor(), but not for another function that is defined in concol.h, setcolor. I suspect that the problem is my file linking. Can anyone help? Thanks.
Usually the compiler reports in which modules there are multiple definitions of a function. Read the compiler messages.
I did... but I don't know what the Z9textcolorv and Z9backcolorv functions are or why they are created. This is what I see when I attempt to compile:
http://postimage.org/image/w6hbmrdxz/full/
Are you asking me what the Z9textcolorv and Z9backcolorv functions are or why they are created?! Is it your code?
Yes, it is my code. That's why it's so confusing to have these mysterious duplicates! As a test, I changed the name of textcolor() in concol.h to xtextcolor(). Upon rebuilding, the compiler said the duplicate was Z9xtextcolor. So it seems the compiler is making this copy for some reason.

Just to be clear, if I search for 'Z9textcolor' in my project, there are 0 results. As you can see in the compiler error in the image above, these strange functions are found in char_traits.h, which I did not create, but seem to be included with C::B or something.
It's just a name that uses the linker
Post lines 61, 69 of concol.h

It would be a lot less work to copy-paste the messages as text.
The compiler can not include headers that are not explicitly or implicitly through other headers specified by you.
So you should find where is the header char_traits.h included.
Line 61 is the opening brace for the function textcolor(). Line 69 is the opening brace for the function backcolor(). The code for concol.h originated here: http://www.cplusplus.com/articles/2ywTURfi/ where lines 41 and 49 correspond to 61 and 69 in my code (I added some comments to the top).

Line 258 of char_traits.h is the middle line of this stuff (which, as I said, I did not write, but got pulled in):
1
2
3
static int
      compare(const char_type* __s1, const char_type* __s2, size_t __n)
      { return __builtin_memcmp(__s1, __s2, __n); }


Oh, and I didn't copy-paste because Code::Blocks doesn't allow me to copy all of the text in that section.
Right click, copy contents to clipboard.

Either put the definitions in `concol.cpp' or inline them. As it is, both `main.cpp' and `somefunctions.cpp' have their own definition (the same) and that confuses the linker.
Well, I wasn't exactly able to resolve the problem, but I was able to work around it, which is good enough for me right now. I just cannibalized concol.h and put the prototypes in item.h and the definitions in somefunctions.cpp, then removed concol.h from the project.

This has pretty much the same effect as what ne555 suggested (thanks!): Splitting up concol.h and including the definitions in concol.cpp. I have never used the inline function other than in concol.h, which I found, so I will make a point to learn about it for future use.
Topic archived. No new replies allowed.