简体   繁体   中英

Wrapping c++ class for python (with opencv), gives “undefined symbol”

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!

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM