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
#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:
//////////////////////////////////////////
// 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)
#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
}
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.
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?