3

I am trying to have a C program call a function in a C++ file that uses OpenCV. I can get the C file to call a basic integer function in a C++ file and return a result, but whenever I try to add some OpenCV code to the C++ file, I get compilation errors. Here is my simple code in each respective module:

foo.cpp

#include <time.h>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/mat.hpp"
#include "opencv/cv.hpp"

#include <iostream>

#ifdef _cplusplus 
extern "C" int f(int);
#endif 

using namespace std;
using namespace cv;

int f(int i)
{
cout << "CPP SON: " << i << endl;
i--;

//Mat image;

//image = imread( "image1.jpg", 1 );

//namedWindow( "image1.jpg", CV_WINDOW_AUTOSIZE );

//imshow( "image1.jpg", image );

//waitKey(0);

return i;
}

bar.c

#include <stdio.h>

int global = 0;

int f(int);

void cc(int i)
{
    global = f(i);
    /* ... */
    printf("hello from C! %d \n", global);
}

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

    printf("this si is the C code called main\n");
    cc(32);


}

Makefile

mot : foo.o bar.o
    g++ `pkg-config --cflags opencv` -o mot `pkg-config --libs opencv` foo.o bar.o

foo.o : foo.cpp
    g++ `pkg-config --cflags opencv` -c foo.cpp `pkg-config --libs opencv` -o foo.o

bar.o : bar.c
    g++ `pkg-config --cflags opencv` -c bar.c `pkg-config --libs opencv` -o bar.o

clean : 
    rm foo.o
    rm bar.o
    rm mot

When commenting out all lines of OpenCV in the foo.cpp file, the following output is returned:

bi@rtes4:~/Desktop$ make
g++ `pkg-config --cflags opencv` -c foo.cpp `pkg-config --libs opencv` -o foo.o
g++ `pkg-config --cflags opencv` -c bar.c `pkg-config --libs opencv` -o bar.o
g++ `pkg-config --cflags opencv` -o mot `pkg-config --libs opencv` foo.o bar.o
bi@rtes4:~/Desktop$ ./mot
this si is the C code called main
CPP SON: 32
hello from C! 31 

Upon commenting out the line "Mat image;" yields the following error:

bi@rtes4:~/Desktop$ make
g++ `pkg-config --cflags opencv` -c foo.cpp `pkg-config --libs opencv` -o foo.o
g++ `pkg-config --cflags opencv` -c bar.c `pkg-config --libs opencv` -o bar.o
g++ `pkg-config --cflags opencv` -o mot `pkg-config --libs opencv` foo.o bar.o
foo.o: In function `cv::Mat::~Mat()':
foo.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
foo.o: In function `cv::Mat::release()':
foo.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x47): undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
make: *** [mot] Error 1

Does anybody have any help they can lend? Thank you kindly in advance for your assistance with this matter, it is greatly appreciated.

MSalters
  • 173,980
  • 10
  • 155
  • 350

2 Answers2

1

It's not a compilation error, but a linking error.

I suspect that you're not familiar with the build phases of C++. Your makefile has rules for compiling (foo.o) and for linking (mot). Both now contain pkg-config --libs opencv. Yet you should only pass the library when linking; the compilation step needs just the OpenCV headers.

What's the output of pkg-config --libs opencv? That's a shell command to obtain your local OpenCV installation. It's probably configured wrong or not at all.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • I get the following output: -L/usr/local/lib -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core -lopencv_hal – Novice Programmer Jul 13 '15 at 21:41
  • Looks OK; is `libopencv_core` actually in `/usr/local/lib` ? – MSalters Jul 14 '15 at 07:13
0

The problem was with the linking phase. I had to pass along the shared library to the gcc compiler so that the C program would be able to reference the C++ code that has not been mangled.