I am trying to call a c++ code from a python script using cython. I already managed to work with an example from here but the thing is: my c++ code includes non-standard libraries from opencv. I believe I am not linking them correctly so I need someone to have a look on my setup.py and my cpp_rect.h and cpp_rect.cpp files.
The error I am getting is regarding to the bold line yn the *.cpp file: cv::Mat img1(7,7,CV_32FC2,Scalar(1,3)); When I try to test the library, I receive an include error when I execute $ python userect.py
:
Traceback (most recent call last):
File "userect.py", line 2, in <module>
from rectangle import Rectangle
ImportError: dlopen(/Users/marcelosalloum/Desktop/python_cpp_interface/rectangle.so, 2): Symbol not found: __ZN2cv3Mat10deallocateEv
Referenced from: /Users/marcelosalloum/Desktop/python_cpp_interface/rectangle.so
Expected in: flat namespace
in /Users/marcelosalloum/Desktop/python_cpp_interface/rectangle.so
The symbol not found (__ZN2cv3Mat10deallocateEv) has something to do with the cv::Mat::deallocate()
function, what indicates that my imports are not working properly.
Any ideas?
My other classes are the following:
This is my setup.py file. Note taht I already included 2 directories although not sure if I did correctly:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
setup(
name = 'Demos',
ext_modules=[
Extension("rectangle",
sources=["rectangle.pyx", "cpp_rect.cpp"], # Note, you can link against a c++ library instead of including the source
include_dirs=[".", "/usr/local/include/opencv/", "/usr/local/include/"],
language="c++"),
],
cmdclass = {'build_ext': build_ext},
)
My cpp_rect.h file includes a cv.h and a namespace cv, as shown bellow:
#include "source/AntiShake.h"
#include <iostream>
#include "cv.h"
using namespace cv;
class Rectangle {
public:
int x0, y0, x1, y1;
Rectangle();
Rectangle(int x0, int y0, int x1, int y1);
~Rectangle();
int getLength();
int getHeight();
int getArea();
void move(int dx, int dy);
**void openCV();**
Rectangle operator+(const Rectangle& other);
};
and my openCV() function simply instantiates an cv::Mat from opencv (file cpp_rect.cpp):
#include "cpp_rect.h"
Rectangle::Rectangle() {
x0 = y0 = x1 = y1 = 0;
}
Rectangle::Rectangle(int a, int b, int c, int d) {
x0 = a;
y0 = b;
x1 = c;
y1 = d;
}
Rectangle::~Rectangle() {
}
void Rectangle::openCV(){
**cv::Mat img1(7,7,CV_32FC2,Scalar(1,3));**
}
...
I can compile the file with the following command: $ python setup.py build_ext --inplace
, which provides me the *.so file. But when I run my userect.py script, I get that include error described firstly in this question.
Any Ideas?
__ZN2cv3Mat10deallocateEv)
mentioned above iscv::Mat::deallocate()
: you can useecho __ZN2cv3Mat10deallocateEv) | c++filt
to find out. If that function is from an external library, I would expect that you need to specify the libraries you need to be linked in yoursetup.py
but I'm not a cython/python user so I don't know the details. – Dietmar Kühlinclude_dirs
is normally what become argument to the-I
compiler options. You probably want to also specifylib_dirs
for the location of the compiled library code andlibraries
(or whatever) to list the required libraries. How these are to be configured exactly, I don't know. In Makefiles these would be part of theLDFLAGS
(e.g.-L<directory-where-the-libraries-are-located>
) and theLDLIBS
(e.g.-l<library-to-be-linked-to>
). – Dietmar Kühl