Cython, C++ and gsl

Cython, C++ and gsl

本文关键字:gsl and C++ Cython      更新时间:2023-10-16

所以我用class.cpp class.h设置了一个c ++类.class.cpp使用了gsl的一些函数(它有#include <gsl/gsl_blas.h>)我将其链接到另一个 c++ 文件主文件没有问题.cpp我可以编译它

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

此外,在没有在class.cpp中包含gsl库的情况下,我设法创建了一个cython文件,该文件在class.cpp中使用我的类,并且可以工作。

但是,当我尝试这两者结合起来时(即在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 文件的开头。

感谢您的任何帮助!

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

根据您的需求进行自定义,但它应该以这种方式工作。

希望这能有所帮助...

您可以使用

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 文档