用Cython包装C ++ lib [英] Wrap C++ lib with Cython

查看:123
本文介绍了用Cython包装C ++ lib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to Cython and I'm trying to use Cython to wrap a C/C++ static library. I made a simple example as follow.

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 ++进行编译和构建。

Then I used g++ to compile and build it.

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

现在我有了一个名为 libtest.a 的静态库。

So now I got a static library called 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'] 。它给了我同样的错误。

I also tried to use libraries=['test'] instead of libraries=['libtest']. It gave me the same errors.

有什么线索吗?

推荐答案

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

If your C++ code is only used by the wrapper, another option is to let the setup compile your .cpp file, like this:

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 参数您的扩展名

For linking to a static library you have to use the extra_objects argument in your 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
)

这篇关于用Cython包装C ++ lib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆