[英]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. 背景:我正在尝试包装c ++类,以便可以从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. 但是,一旦我从opencv中使用了任何东西(例如:“ cv :: Mat frame;”,我就会收到“未定义符号”错误。当我删除行“ cv :: VideoCapture wcam;”时,所有内容都会编译并执行应该。
What am I doing wrong? 我究竟做错了什么?
webcam.cpp: 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: 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) (我知道可以在python中直接使用openCV)
UPDATE 更新
I found that using "cv::Mat frame" is OK. 我发现使用“ cv :: Mat框架”是可以的。 but "cv::VideoCapture webcam" is not. 但不是“ cv :: VideoCapture网络摄像头”。
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? 无论如何,由于您已经在using namespace cv
指令,您是否尝试仅删除cv::
部分?
Also I'm not sure how cv
and cv2
like each other? 另外我不确定cv
和cv2
如何cv2
相似? 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.