Functions and Object Files C++ will NOT Compile

I have a project I'm coding in C++ with the OpenCV library. I want to separate the project into (right now at least) 6 files. I have xPush.cpp xPush.h ColorPick.cpp ColorPick.h ColorExtract.cpp ColorExtract.h

ColorPick.h has the following code:
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
#ifndef STDX
#define STDX
#include <stdio.h>
#include <stdlib.h>
#include <cv.h>
#include <cvaux.h>
#include <highgui.h>
#include <math.h>
#include <stdlib.h>
#endif

// Check to see if this header was called already
#ifndef COLORPICK_H
// Define
#define COLORPICK_H

// Declarations for ColorPick
IplImage *cp_LIVE;
IplImage *cp_EX;
int color;
int end;
int key;

// Class passes color thresholds back and forth (BGR format)

class cThresh {
	public:
	int mC;
	int Cp1;
	int Cp2;
	};
	
#endif 


It is called in xPush.h which has the following code:
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
#ifndef STDX
#define STDX
#include <stdio.h>
#include <stdlib.h>
#include <cv.h>
#include <cvaux.h>
#include <highgui.h>
#include <math.h>
#include <stdlib.h>
#endif


// Check to see if this has already been included
#ifndef XPUSH_H
// Define
#define XPUSH_H

#include "ColorPick.h"
#include "ColorExtract.h"

// Declare IplImage variables for xPush
IplImage *img_LIVE=NULL;		// Live camera capture image
IplImage *img_CURR=NULL;		// Current camera frame
IplImage *img_COLX=NULL;		// Extracted Color Image

// Declare Color Thresh objects
cThresh Color1;
cThresh Color2;



#endif 


This is in turn called by xPush.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
//////////////////////////////////////////
//	Main C++ file for xPush		//
//////////////////////////////////////////

#include "xPush.h"

// Prototype Additional C++ files
int ColorExtract(IplImage *img_SOURCE, IplImage *img_DEST);
int ColorPick(cThresh Color1, cThresh Color2);




int tester;

// Main function call

int main( int argc, char **argv){

// First call ColorPick so we can chose the colors to be used
tester = ColorPick(Color1, Color2);













/////////////////////////////////
// Below curly ends main function
}


The last important file is ColorPick.cpp (function doesn't really use inputs right now)
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
#include "ColorPick.h"

int ColorPick(cThresh C1, cThresh C2){

// Begin by calling up a live feed
CvCapture *cpLiveFeed=NULL;		// Capture placeholder (improves speed)
cpLiveFeed=cvCaptureFromCAM(0);		// Set up the area for captured frames

// Loop for feed
while (end != 1){

// Capture frames
cvGrabFrame (cpLiveFeed);
cp_LIVE = cvRetrieveFrame (cpLiveFeed);

// Image Creation
cp_EX=cvCreateImage(cvGetSize(cp_LIVE),8,1);





// Display windows
cvNamedWindow("Live",CV_WINDOW_AUTOSIZE);
	cvShowImage("Live",cp_LIVE);
cvNamedWindow("Color Extraction",CV_WINDOW_AUTOSIZE);
	cvShowImage("Color Extraction",cp_EX);
	
	
// Create Trackbars on Live	
cvCreateTrackbar("Main Color Channel","Live",&color,2,NULL);	

// Wait for Kepress to finish the color threshold selection
key=cvWaitKey(20);
if (key != -1)
end = 1;


// Below ends live feed while loop
}




return 1;
/////////////////////////////
// Below curly ends ColorPick
}


The makefile is:
1
2
3
4
5
6
7
8
xPush: xPush.cpp xPush.h ColorExtract.o ColorPick.o
	g++ xPush.cpp ColorExtract.o ColorPick.o `pkg-config --libs --cflags opencv` -o xPush

ColorExtract.o: ColorExtract.cpp ColorExtract.h
	g++ -c ColorExtract.cpp `pkg-config --libs --cflags opencv` -o ColorExtract.o
	
ColorPick.o: ColorPick.cpp ColorPick.h
	g++ -c ColorPick.cpp `pkg-config --libs --cflags opencv` -o ColorPick.o



And the g++ output is:
 make
g++ -c ColorExtract.cpp `pkg-config --libs --cflags opencv` -o ColorExtract.o
g++ -c ColorPick.cpp `pkg-config --libs --cflags opencv` -o ColorPick.o
g++ xPush.cpp ColorExtract.o ColorPick.o `pkg-config --libs --cflags opencv` -o xPush
ColorPick.o:(.bss+0x0): multiple definition of `cp_LIVE'
/tmp/ccqugB5e.o:(.bss+0x0): first defined here
ColorPick.o:(.bss+0x4): multiple definition of `cp_EX'
/tmp/ccqugB5e.o:(.bss+0x4): first defined here
ColorPick.o:(.bss+0x8): multiple definition of `color'
/tmp/ccqugB5e.o:(.bss+0x8): first defined here
ColorPick.o:(.bss+0xc): multiple definition of `end'
/tmp/ccqugB5e.o:(.bss+0xc): first defined here
ColorPick.o:(.bss+0x10): multiple definition of `key'
/tmp/ccqugB5e.o:(.bss+0x10): first defined here
/tmp/ccqugB5e.o: In function `main':
xPush.cpp:(.text+0x3f): undefined reference to `ColorPick(cThresh, cThresh)'
collect2: ld returned 1 exit status
make: *** [xPush] Error 1



As far as I can tell, I'm doing everything correctly, but apparently g++ doesnt like one of my #ifndef statements or definitions or something.

Any suggestions?
Last edited on
You are defining global variables multiple times (by including the .h files in different .cpp files), which is why you are getting the error.

For example:
1
2
3
4
5
//some_include.h
#ifndef myinc
#define myinc
int some_global;
#endif 


1
2
//1.cpp
#include "some_include.h" 


1
2
//2.cpp
#include "some_include.h" //Error, some_global is defined twice when you link them together 
It's probably a good idea to use the extern keyword, and define your variables in ONE cpp file.

-Albatross
Thank you both. My solution was to take the function specific declarations and put them into the .cpp file instead of the .h file.

Can I keep class definitions in an .h file? For instance can i leave the cThresh class inside ColorPick.h? It lets me compile and run, so I'm guessing its fine, Im just asking if thats the best practice, or if I should create a separate class.h to contain all of my classes? Is there a best practice?

Thanks so much for your quick replies!
Topic archived. No new replies allowed.