Reputation: 3269
Background: I am trying to wrap a c++ class, so that I can use it from python. But as soon as I use anything from opencv (like: "cv::Mat frame;" I get an "undefined symbol" error. As soon as I remove the line "cv::VideoCapture wcam;" everything compiles and executes as it should.
What am I doing wrong?
webcam.cpp:
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
using namespace cv;
using namespace std;
class webcam{
private:
cv::VideoCapture wcam;
public:
webcam();
void nextFrame();
//cv::Mat getNewFrame();
};
webcam::webcam(){}
void webcam::nextFrame(){
std::cout << "TESTING TESTING";
}
extern "C" {
webcam* webcam_new(){ return new webcam(); }
void test(webcam* wc) {wc->nextFrame();}
//void Foo_bar(Foo* camCon){ foo->bar(); }
}
compiling:
g++ -c -fPIC webcam.cpp -o webcam.o -lopencv_core -lopencv_highgui
g++ -shared -Wl,-soname,webcam.so -o webcam.so webcam.o
cam.py:
import cv2
import numpy as np
from ctypes import cdll
lib = cdll.LoadLibrary('./webcam.so')
class camCon(object):
def __init__(self):
self.obj = lib.webcam_new()
def test(self):
lib.test(self.obj)
fooo = camCon()
fooo.test()
Error output:
Traceback (most recent call last):
File "wrapp.py", line 5, in <module>
lib = cdll.LoadLibrary('./webcam.so')
File "/usr/lib/python2.7/ctypes/__init__.py", line 443, in LoadLibrary
return self._dlltype(name)
File "/usr/lib/python2.7/ctypes/__init__.py", line 365, in __init__
self._handle = _dlopen(self._name, mode)
OSError: ./webcam.so: undefined symbol: _ZN2cv12VideoCaptureC1Ev
(I am aware that it is possible to use openCV in python directly)
UPDATE
I found that using "cv::Mat frame" is OK. but "cv::VideoCapture webcam" is not.
Thanks!
Upvotes: 0
Views: 1085
Reputation: 3094
I know it's stupid and I'd like to add a comment but my rep is bad....
Anyhow, did you try just removing the cv::
part since you're already using the using namespace cv
directive?
Also I'm not sure how cv
and cv2
like each other? I'm not quite sure they are the same thing.
Upvotes: 1
Reputation: 3269
Solved:
When compiling for a shared lib I had to add the libraries like this:
g++ -shared -Wl,-soname,webcam.so -o webcam.so webcam.o -lopencv_core -lopencv_highgui -lopencv_imgproc
Upvotes: 1