Sunday, October 4, 2015

Compilation of OpenCV programs

Compiling programs written in C++ is usually a fairly easy and non-complex task to complete.

% g++ -o program program.cpp

However, OpenCV (Open Source Computer Vision) employs a much more complex set of libraries that are necessary to reference object compilation.  While a good amount of documentation exists to manipulate and generate a useful program to generate desired outcomes, some of it remains an exercise to the programmer to know how to get it setup and running.  Just about anyone that has that level of knowledge can their code to work just fine.  However, sometimes the obvious slips by.

So, you're working on your program and it's based off of some known, good working code.  You set your parameters and hit the compilation string.

/tmp/ccSv9hPY.o: In function `MatchingMethod(int, void*)':
/home/user/Project/strimaker/template/MatchTemplate_Demo.cpp:87: undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
/home/user/Project/strimaker/template/MatchTemplate_Demo.cpp:88: undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
/tmp/ccSv9hPY.o: In function `main':
/home/user/Project/strimaker/template/MatchTemplate_Demo.cpp:37: undefined reference to `cv::namedWindow(cv::String const&, int)'
/home/user/Project/strimaker/template/MatchTemplate_Demo.cpp:38: undefined reference to `cv::namedWindow(cv::String const&, int)'
/home/user/Project/strimaker/template/MatchTemplate_Demo.cpp:42: undefined reference to `cv::createTrackbar(cv::String const&, cv::String const&, int*, int, void (*)(int, void*), void*)'
/home/user/Project/strimaker/template/MatchTemplate_Demo.cpp:46: undefined reference to `cv::waitKey(int)'
collect2: error: ld returned 1 exit status

Now you've got a slew of problems and a non-existent binary.

Linking the libraries is fairly simple, as long as you know what to link against.

 g++ -o MatchTemplate_Demo MatchTemplate_Demo.cpp -g -O2 -lopencv_imgcodecs -lopencv_imgproc -lopencv_core -lopencv_highgui

Here, I'm linking against anything the headers require.  One way to figure out what libraries to use is through the use of the Autotools program.  Unfortunately, OpenCV relies on CMake, and so one must be able to distinguish between them in order to avoid wasting time on a problem that may not need to exist.