[英]Wrapping c++ class for python (with opencv), gives ERROR
I am trying to generate C++ code in a .so file and to import the .so file in python. 我正在尝试在.so文件中生成C ++代码,并在python中导入.so文件。 My C++ code is
我的C ++代码是
extern "C" int top(int a, int b){
return a + b;
}
extern "C" int fark(int a, int b){
return a - b;
}
extern "C" int carp(int a, int b){
return a * b;
}
extern "C" int bol(int a, int b){
return a / b;
}
extern "C" void foto(string s)
{
Mat im = imread(s, 1);
if (im.empty())
{
cout << "url hatali" << endl;
}
else
{
imshow("foto", im);
waitKey(1000);
}
}
extern "C" void gri(string s){
Mat im = imread(s, 1);
if (im.empty())
{
cout << "url hatali" << endl;
}
else
{
cvtColor(im, im, CV_RGB2GRAY);
imshow("Gri", im);
waitKey(1000);
}
}
extern "C" void asdf(string s ,int i){
Mat im = imread(s, 1);
if (im.empty())
{
cout << "url hatali" << endl;
}
else
{
cvtColor(im, im, CV_RGB2GRAY);
threshold(im, im, i, 255, THRESH_BINARY);
imshow("Binary", im);
waitKey(1000);
}
}
My generate command is : g++ -c -fPIC webcam.cpp -o webcam.o -lopencv_core -lopencv_highgui g++ -shared -Wl,-soname,webcam.so -o webcam.so webcam.o 我的生成命令是:g ++ -c -fPIC webcam.cpp -o webcam.o -lopencv_core -lopencv_highgui g ++ -shared -Wl,-soname,webcam.so -o webcam.so webcam.o
and I generate .so file but when I import my .so file in my python code I get the error : _ZN2cv6imshowERKNS_6StringERKNS_11_InputArrayE 并且生成.so文件,但是当我在python代码中导入.so文件时,出现错误:_ZN2cv6imshowERKNS_6StringERKNS_11_InputArrayE
My python code is: 我的python代码是:
from ctypes import cdll
mydll=cdll.LoadLibrary('/PATH/deneme.so')
print(mydll.top(123,123))
print(mydll.carp(123,123))
print(mydll.fark(123,123))
print(mydll.bol(123,123))
My generate command is : g++ -c -fPIC webcam.cpp -o webcam.o -lopencv_core -lopencv_highgui g++ -shared -Wl,-soname,webcam.so -o webcam.so webcam.o
我的生成命令是:g ++ -c -fPIC webcam.cpp -o webcam.o -lopencv_core -lopencv_highgui g ++ -shared -Wl,-soname,webcam.so -o webcam.so webcam.o
You need to put the libraries in the link step, not the compile step. 您需要将库放入链接步骤,而不是编译步骤。
g++ -c -fPIC webcam.cpp -o webcam.o
g++ -fPIC -shared -Wl,-soname,webcam.so -o webcam.so webcam.o -lopencv_core -lopencv_highgui
As @πάντα ῥεῖ noted, you should be careful about putting a std::string
in the interface of an extern "C"
function. 正如@πάνταῥεῖ指出的那样,您应该小心在
extern "C"
函数的接口中放置std::string
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.