Cython, C ++ and gsl

So, I created a C ++ class with class class.cpp, class.h. class.cpp uses some functions from gsl (it has #include <gsl/gsl_blas.h> ) I have no problem linking this to another C ++ file main.cpp, and I can compile it with

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

Also, without including gsl libary in class.cpp, I was able to create a cython file that uses my class in class.cpp and it works.

However, when I try to combine the two (that is, using the C ++ class in cython, where the C ++ class uses gsl functions), I don't know what to do. I think I should turn on

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

somewhere in the setup file, but I don’t know where and how. My setup.py looks like

 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'), ) 

and I

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

at the beginning of my .pyx file.

Thanks for any help!

+4
source share
2 answers

I suggest you use the extra_compile_args parameter in your extension. I already wrote some answer, which, fortunately, uses the GSL dependency here .

Customize it according to your needs, but it should work this way.

Hope this helps ...

+1
source

You can specify the necessary external libraries using the libraries , include_dirs and library_dirs of the Extension class. For instance:

 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])) 

Here the C ++ class is defined in myext.cpp and the cython interface in myext.pyx . See Also: Cython Documentation

+1
source

Source: https://habr.com/ru/post/1502180/


All Articles