Cython, C++ and gsl

4

所以我已经建立了一个C++类,例如class.cpp,class.h。class.cpp使用gsl中的一些函数(它有#include <gsl/gsl_blas.h>)。 我没有问题将其链接到另一个C++文件main.cpp,并且我可以编译它。

g++ -o main main.o class.o  -I/home/gsl/include -lm -L/home/gsl/lib -lgsl -lgslcblas

此外,在class.cpp中没有包含gsl库的情况下,我已经成功创建了一个使用class.cpp中类的cython文件。

但是,当我试图将这两者结合起来(即在cython中使用C++类,其中C++类使用gsl函数)时,我不知道该怎么办。我猜我必须要包含

I/home/gsl/include -lm -L/home/gsl/lib -lgsl -lgslcblas

在设置文件中的某个地方,但我不知道在哪里或如何设置。我的setup.py文件看起来像:

from distutils.core import setup
from Cython.Build import cythonize

import os

os.environ["CC"] = "g++"
os.environ["CXX"] = "g++"

setup(
    name = "cy",
    ext_modules = cythonize('cy.pyx'),
)

我有

# distutils: language = c++
# distutils: sources = class.cpp

在我的.pyx文件开头。

非常感谢您的帮助!

2个回答

1
你可以使用 Extension 类的 librariesinclude_dirslibrary_dirs 参数指定所需的外部库。例如:
from distutils.extension import Extension
from distutils.core import setup
from Cython.Build import cythonize
import numpy

myext = Extension("myext",
                  sources=['myext.pyx', 'myext.cpp'],
                  include_dirs=[numpy.get_include(), '/path/to/gsl/include'],
                  library_dirs=['/path/to/gsl/lib'],
                  libraries=['m', 'gsl', 'gslcblas'],
                  language='c++',
                  extra_compile_args=["-std=c++11"],
                  extra_link_args=["-std=c++11"])
setup(name='myproject',
      ext_modules=cythonize([myext]))

这里,C++类在myext.cpp中定义,而Cython接口在myext.pyx中定义。参见: Cython文档


1

我建议您在扩展中使用extra_compile_args选项。我已经写了一些答案,幸运的是这个答案使用了GSL依赖here

根据您的需求进行定制,但应该可以这样工作。

希望这可以帮到您...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接