简体   繁体   中英

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. My C++ code is

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

and I generate .so file but when I import my .so file in my python code I get the error : _ZN2cv6imshowERKNS_6StringERKNS_11_InputArrayE

My python code is:

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

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.

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