python - 用 Cython 包装 C++ 库

标签 python cython

我是 Cython 的新手,我正在尝试使用 Cython 来包装 C/C++ 静态库。我做了一个简单的例子如下。

Test.h:

#ifndef TEST_H
#define TEST_H

int add(int a, int b);
int multipy(int a, int b);

#endif

Test.cpp

#include "test.h"
int add(int a, int b)
{
    return a+b;

}

int multipy(int a, int b)
{
    return a*b;
} 

然后我用g++编译构建它。

g++ -c test.cpp -o libtest.o
ar rcs libtest.a libtest.o

所以现在我得到了一个名为 libtest.a 的静态库。

Test.pyx:

cdef extern from "test.h":
        int add(int a,int b)
        int multipy(int a,int b)

print add(2,3)

Setup.py:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [Extension("test",
                     ["test.pyx"],
                     language='c++',
                     include_dirs=[r'.'],
                     library_dirs=[r'.'],
                     libraries=['libtest']
                     )]

setup(
  name = 'test',
  cmdclass = {'build_ext': build_ext},
  ext_modules = ext_modules
)

我打来的电话:

python setup.py build_ext --compiler=mingw32 --inplace

输出是:

running build_ext
cythoning test.pyx to test.cpp
building 'test' extension
creating build
creating build\temp.win32-2.6
creating build\temp.win32-2.6\Release
C:\Program Files\pythonxy\mingw\bin\gcc.exe -mno-cygwin -mdll -O -Wall -I. -IC:\
Python26\include -IC:\Python26\PC -c test.cpp -o build\temp.win32-2.6\Release\test.o
writing build\temp.win32-2.6\Release\test.def
C:\Program Files\pythonxy\mingw\bin\g++.exe -mno-cygwin -mdll -static --entry _D
llMain@12 --output-lib build\temp.win32-2.6\Release\libtest.a --def build\temp.w
in32-2.6\Release\test.def -s build\temp.win32-2.6\Release\test.o -L. -LC:\Python
26\libs -LC:\Python26\PCbuild -ltest -lpython26 -lmsvcr90 -o test.pyd
g++: build\temp.win32-2.6\Release\libtest.a: No such file or directory
error: command 'g++' failed with exit status 1

我也尝试使用 libraries=['test'] 而不是 libraries=['libtest']。它给了我同样的错误。

有什么线索吗?

最佳答案

如果您的 C++ 代码仅由包装器使用,另一种选择是让安装程序编译您的 .cpp 文件,如下所示:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [Extension("test",
                     ["test.pyx", "test.cpp"],
                     language='c++',
                     )]

setup(
  name = 'test',
  cmdclass = {'build_ext': build_ext},
  ext_modules = ext_modules
)

要链接到静态库,您必须使用 extra_objects Extension 中的参数:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [Extension("test",
                     ["test.pyx"],
                     language='c++',
                     extra_objects=["libtest.a"],
                     )]

setup(
  name = 'test',
  cmdclass = {'build_ext': build_ext},
  ext_modules = ext_modules
)

关于python - 用 Cython 包装 C++ 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2105508/

相关文章:

c++ - gcc 编译的 C++ 中不包含调试符号

python - Python 中的解释与动态调度惩罚

python - Flask 不渲染模板,可能是文件结构的问题

python - 通过将元音替换为字符串中的索引来删除元音

python - docker-py client.images.list() 始终显示所有图像

python - react 扩散算法中的 Numba 或 Cython 加速

Python Pandas : Select columns that their content does not contain a value

Python:基于相同元素对多个列表进行分组

python - Cython:如何解决 TypeError:无法将 memoryviewslice 转换为 numpy.ndarray?

python - Cython - 在 for 循环中使用 "from"关键字