"multiple definition of " error

I´m trying to make a control for an hexapod robot. This is not a problem of robotics but an C++ implementation. Any help will be appreciated!!!
Note: "I needed to remove some functions to get 9000 characters!"
(If any was interested in the code, ask for it!)

Command to compile:

Makefile:
1
2
3
4
5
6
7
8
9
10
all: libs

libs:	hexabot.o
	g++  -Wall -c hexabot.cpp
	rm -f libhexabot.a
	ar -cvq libhexabot.a hexabot.o
	g++ main.cpp -o code libhexabot.a
clean:
	rm -f *.o
	rm -f *.a


Error:
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
make
g++  -Wall -c hexabot.cpp
rm -f libhexabot.a
ar -cvq libhexabot.a hexabot.o
a - hexabot.o
g++ main.cpp -o code libhexabot.a
libhexabot.a(hexabot.o): In function `hfiles::viewdata()':
hexabot.cpp:(.text+0x316): multiple definition of `hfiles::viewdata()'
/tmp/ccGS6Ljw.o:main.cpp:(.text+0x0): first defined here
libhexabot.a(hexabot.o): In function `hfiles::savedata(char const*)':
hexabot.cpp:(.text+0x35c): multiple definition of `hfiles::savedata(char const*)'
/tmp/ccGS6Ljw.o:main.cpp:(.text+0x46): first defined here
libhexabot.a(hexabot.o): In function `hfiles::~hfiles()':
hexabot.cpp:(.text+0x1dbc): multiple definition of `hfiles::~hfiles()'
/tmp/ccGS6Ljw.o:main.cpp:(.text+0x14e): first defined here
libhexabot.a(hexabot.o): In function `hfiles::~hfiles()':
hexabot.cpp:(.text+0x1eba): multiple definition of `hfiles::~hfiles()'
/tmp/ccGS6Ljw.o:main.cpp:(.text+0x24c): first defined here
libhexabot.a(hexabot.o): In function `hfiles::hfiles()':
hexabot.cpp:(.text+0x1fb8): multiple definition of `hfiles::hfiles()'
/tmp/ccGS6Ljw.o:main.cpp:(.text+0x3f4): first defined here
libhexabot.a(hexabot.o): In function `hfiles::hfiles()':
hexabot.cpp:(.text+0x204c): multiple definition of `hfiles::hfiles()'
/tmp/ccGS6Ljw.o:main.cpp:(.text+0x488): first defined here
libhexabot.a(hexabot.o): In function `hfiles::readcfg(char const*)':
hexabot.cpp:(.text+0x24e8): multiple definition of `hfiles::readcfg(char const*)'
/tmp/ccGS6Ljw.o:main.cpp:(.text+0x51c): first defined here
collect2: ld returned 1 exit status
make: *** [libs] Error 1



hexapod.hpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#ifndef _HEXABOT_CORE
#define _HEXABOT_CORE
/* "Defines" para el puerto serial*/
#define __RW 3
#define __R  1
#define __W  2
#define  ON  1
#define  OFF 0
#define  __SRAD 13.7
#define Pi 3.1416
#define pi 3.1416

#ifndef _HFILES_H
 #include "hfiles.cpp"
#endif

using namespace std;
class hexapod: public hfiles
{
	private:
		//hfiles t_file;
		char __offset[26];
		float __l1, __l2, __l3;
		float theta;
		char fdone;
		char storing;
		bool recording;
		float roundt(float val);
		float updist;
		ofstream sout;
	public:	
		int mspeed;
		int sspeed;	
		float X, Y, Z;
		float lX,lY,lZ;	
		float sX, sY, sZ;

		bool protection;
		char angoff;
		const char* _Serial_Device_Name;
 		hexapod(const char* __SERIAL_PORT_DEVICE,
		    const char* __BAUDRATE);
		~hexapod(){};                                      
		void store_positions(double x, double y, double z);
		void updistance(float distance, float angle);	
		void rotate(float angle);
	public:
		char Open(char);					
		void Close(void);					
		void stop(void);					
		void halt_motor(char motor);
		void fixmotors(int offset0, int offset1, int offset2, 	
		    int offset4, int offset5, int offset6, int offset8,
		    int offset9, int offset10, int offset16, int offset17,
		    int offset18, int offset20, int offset21, int offset22,
		    int offset24, int offset25, int offset26, float link1,
		    float link2, float link3);
		void set_store(bool answ);				
		void storethis(void);
		void storethis(double x, double y, double z);
		void set_angle(char motor, double angle);		
		void set_feet(float angle);				
		void set_knees(float angle);				
		void set_shoulders(float angle);

		 void set_knees(int angle);				
		 void set_shoulders(int angle);				
		 void set_leg(char leg, double x, double y, double z);
		 void set_gleg(char leg, double x, double y, double z);
									
		 void set_legs(double x, double y, double z);		
		 void set_rlegs(double x, double y, double z);		
		 void set_llegs(double x, double y, double z);		
		 void set_rstep(double x, double y, double z);		
		 void set_lstep(double x, double y, double z);		
	 public:
		 void standup(char distance);
		 void sitdown(void);
};
#endif 


hexabot.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#ifndef _STDLIB_H
 #include <stdlib.h>
#endif
#ifndef _SYS_STAT_H
 #include <sys/stat.h>
#endif
#ifndef _GLIBCXX_IOSTREAM
 #include <iostream>
#endif
#ifndef _GLIBCXX_FSTREAM
 #include <fstream>
#endif
#ifndef _GLIBCXX_CSTDLIB
 #include <cstdlib>
#endif
#ifndef _GLIBCXX_STRING
 #include <string>
#endif
#ifndef _GLIBCXX_CCTYPE
 #include <cctype>
#endif
#ifndef _MATH_H
 #include <math.h>
#endif
#ifndef _HEXABOT_H
 #include "hexabot.hpp"
#endif

float hexapod::roundt(float val)
{
	return round(val*10)/10;
}

hexapod::hexapod(const char* __SERIAL_PORT_DEVICE, const char* __BAUDRATE){
	string __system("stty ");
	__system = __system + __BAUDRATE + " < " + __SERIAL_PORT_DEVICE;
	_Serial_Device_Name = __SERIAL_PORT_DEVICE;
	system(__system.c_str());
	//this->getdata();
	//this->viewdata(););
}

/*Open, es una funcion para abrir el archivo o puerto serial en este caso.
 * ejemplo:
 *         if(!obj.open(atributos_de_lectura_o_escritura))
 *              exit(0);  */                                 
char hexapod::Open(char attr){
	struct stat stFileInfo;
	if(stat(_Serial_Device_Name,&stFileInfo)==0)
	{
		if((attr==__R) |(attr== __RW))
			sout.open(_Serial_Device_Name);
		if(sout.is_open())
			return 1;
		return 0;        
	}
	cout<<"2"<<endl;
	sout<<"#"<<"0 p0 S750"<<endl;
	return 2;                    
}

/* Cambia a modo de grabacion en caso de ser verdadero */
void hexapod::set_store(bool answ)
{
	storing=answ;
}

/* Esta funcion cierra el puerto serial*/
void hexapod::Close(void)
{
	stop();
	cout<<"Closing Serial Port"<<std::flush;
	sout.close();
}

void hexapod::stop(void)
{
	char __counter;
	for(__counter=0;__counter<32;__counter++)
	{sout<<"#"<<(int)__counter<<" p0"<<endl;
	}
}

void hexapod::halt_motor(char motor)
{
	sout<<"#"<<(int)motor<<" p0"<<endl;
}

/******************************************************************************
 *       BEGIN WITH THE HEXAPOD CONTROL                                       *
 *****************************************************************************/
/*Con esta funcion se logran corregir los motores, ya que mecanicamente se en-
 cuentran desajustados.*/

void hexapod::fixmotors(int offset0, int offset1, int offset2, int offset4,
    int offset5, int offset6, int offset8, int offset9, int offset10,
    int offset16, int offset17, int offset18, int offset20, int offset21,
    int offset22, int offset24, int offset25, int offset26, float link1,
    float link2, float link3)
{
...
}

/* Se especifica el motor que se quiere mover y un angulo. 
 Algunos de los motores estan en direcciones diferentes
 en el hexapodo, esto es corregido mediante esta funcion.  */
void hexapod::set_angle(char motor, double angle)
{
...
}

void hexapod::set_feet(float angle)
{
...
}
void hexapod::set_knees(float angle)
{
...
}
void hexapod::set_shoulders(float angle)
{
...
}

void hexapod::store_positions(double x, double y, double z)
{
...
}

void hexapod::storethis(void)				
{
...
}

void hexapod::storethis(double x, double y, double z)	
{
...

}

void hexapod::set_leg(char leg, double x, double y, double z)
{
...
}

/*** Se mueven las extremidades basados en el plano global ***/
void hexapod::set_gleg(char leg, double x, double y, double z)
{
...
}
	
void hexapod::set_legs(double x, double y, double z)
{
...		
}

void hexapod::set_rlegs(double x, double y, double z)
{
...		
}

void hexapod::set_llegs(double x, double y, double z)
{
...					
}

void hexapod::set_rstep(double x, double y, double z)
{
...		
}

void hexapod::set_lstep(double x, double y, double z)
{
...		
}

/* Levantamos la arana hasta una altura especificada. */
void hexapod::standup(char distance)	
{
...
}

void hexapod::sitdown(void)
{
...
}

/* Levantamos la arana hasta una altura especificada. */
void hexapod::updistance(float diff, float angle)	
{
...
}

void hexapod::rotate(float angle)	
{
...
}
Line 14 of hexapod.hpp: #include "hfiles.cpp"
Don't include source files. Include only headers. If you are going to include sources, then make sure the are only included in a single compilation unit and that you're not compiling those sources by themselves and linking them into the final executable.

By the way, you don't really need to verify that the compiler has <iostream> if it doesn't have it, any source that actually needs it will fail to compile anyway, so the check is rather pointless.
Dam! I was looking too many times the ".cpp" include but my mind every time saw a ".hpp"... Thanks ...
About the compiler verification, i was trying to disable the possibility of redefine a library. (marked as solved)
Last edited on
Even if you include a standard header more than once, it won't raise an error.
Topic archived. No new replies allowed.