使用Cython和distutilis方法从Python调用C代码
作者:互联网
我正在尝试使用cython从python脚本调用c代码.我已经设法使用here的示例了,但问题是:我的c代码包括来自opencv的非标准库.我相信我没有正确链接它们,因此我需要有人来查看我的setup.py以及cpp_rect.h和cpp_rect.cpp文件.
我遇到的错误与* .cpp文件中的粗体行yn有关:cv :: Mat img1(7,7,CV_32FC2,Scalar(1,3));当我尝试测试库时,在执行$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
找不到符号(__ZN2cv3Mat10deallocateEv)与cv :: Mat :: deallocate()函数有关,这表明我的导入无法正常工作.
有任何想法吗?
我的其他课程如下:
这是我的setup.py文件.请注意,尽管不确定执行是否正确,但我已经包含了2个目录:
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},
)
我的cpp_rect.h文件包括一个cv.h和一个命名空间cv,如下所示:
#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);
};
而我的openCV()函数只是从opencv实例化一个cv :: Mat(文件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));**
}
...
我可以使用以下命令来编译该文件:$python setup.py build_ext –inplace,它为我提供了* .so文件.但是,当我运行userect.py脚本时,出现该问题首先描述的包含错误.
有任何想法吗?
解决方法:
已解决,感谢DietmarKühl的评论和this video from youtube!
什么问题?我发现我的setup.py配置错误.应该是这样的:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
setup(
name = 'DyCppInterface',
version = '1.0',
author = 'Marcelo Salloum dos Santos',
# The ext modules interface the cpp code with the python one:
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=[".","source" , "/opt/local/include/opencv", "/opt/local/include"],
language="c++",
library_dirs=['/opt/local/lib', 'source'],
libraries=['opencv_core', 'LibCppOpenCV'])
],
cmdclass = {'build_ext': build_ext},
)
为了正确配置它,需要注意三件事:
> include_dirs:setup.py或.h和.cpp中的每个引用文件都应在include_dirs中具有其容器文件夹;
> library_dirs:每个引用的库都应在此处编写其容器文件夹;
>图书馆:必须在此处输入图书馆的名称
观看this video on how to use and configure a dynamic library (using Eclipse CDT),可以回答有关如何为cython配置库的其他问题.
标签:opencv,cython,distutils,python,c-4 来源: https://codeday.me/bug/20191122/2063061.html