使用Cython包装C ++库

时间:2010-01-20 22:27:06

标签: 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']。它给了我同样的错误。

关于此的任何线索?

3 个答案:

答案 0 :(得分:24)

如果您的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
)

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

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
)

答案 1 :(得分:4)

您的Test.pyx文件未按预期执行。 print add(2,3)不会调用add() C ++函数;你必须显式创建一个包装函数来做到这一点。 Cython不会自动为您创建包装器。

这样的事情可能就是你想要的:

cdef extern from "test.h":
        int _add "add"(int a,int b)
        int _multiply "multiply"(int a,int b)

def add(a, b):
    return _add(a, b)

def multiply(a, b):
    return _multiply(a, b)

print add(2, 3)

您可以查看Cython的documentation了解更多详情。

答案 2 :(得分:2)

我认为你可以通过指定正确的library_dirs来解决这个具体问题(你实际上 libtest.a - 显然它找不到了),但我想你呢?我还有另一个问题 - 你的入口点没有正确地声明为extern "C",因此函数的名称将被C ++编译器“破坏”(查看从libtest.a导出的名称,你会看到!),所以除了C ++(包括C,Cython等)之外的任何其他语言都会遇到问题。修复方法是将它们声明为extern "C"

相关问题