Organizing a Cython build with setup.py with more than one C++file

809 views
Skip to first unread message

ashwin .D

unread,
May 3, 2019, 4:03:22 AM5/3/19
to cython-users
First let me say that I have my cython code that is working as a result of compiling with

cython --cplus point.pyx 

Now I am trying to get the code organized into a setup.py file so that I can use it a complex project. I am trying to wrap C++ code with Cython so that I
can call it in a Python script(s).

In my working directory - I have several C++ files that can be compiled with g++.

My questions are related to the scope of setup.py. Is it possible to compile the C++ files as part of setup.py or must they be compiled separately ?
Is it a problem that the .pxd and .pyx file be located in the same directory ? The issue with the code below of setup.py file is that the environment of
setup.py seems to think the flags I have added are meant for the Cython code. That is incorrect. It is actually meant for the C++ code.

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
from Cython.Build import cythonize

import os

# Building
setup
(
    cmdclass
= {'build_ext': build_ext},
    ext_modules
= [
   
Extension("p2",
              sources
=["point.pyx", \
                       
"./Point.cpp", \
                       
"./Distance.cpp", \
                       
],
              language
="c++",
              extra_compile_args
=["-Wall","-std=c++17","-I/usr/local/include"],
              extra_link_args
=["-lGeographic","-L/usr/local/lib"],
             
)
   
]
)                  

I have looked at this SO answer - Project Organization With C++ and Cython but I am still not clear on what exactly needs to be done. I am looking for some
sample setup.py file that wrap several C++ files and then use them in a .pyx/.pxd file. Any suggestions ?

Hai Nguyen

unread,
May 3, 2019, 9:53:21 AM5/3/19
to cython...@googlegroups.com
Hi, you can see mdtraj project on github.

Hai


--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ashwin .D

unread,
May 3, 2019, 12:02:00 PM5/3/19
to cython-users
Thanks. I have enclosed the smallest possible code that I believe reproduces my problem and any help will be appreciated.

point.pyx

#distutils : language = c++
#distutils : sources = Point.cpp
from libcpp.memory cimport shared_ptr
from libcpp.vector cimport vector

from point cimport Point

cdef
class PyA:

     
def __cinit__(self):
       
print("hello")

     
def buildPoints(self):

        cdef shared_ptr
[Point] point
        point
= shared_ptr[Point](new SphericalPoint())

and my point.pxd file -

from libcpp.memory cimport shared_ptr
from libcpp.vector cimport vector

cdef
extern from "Point.h":
    cdef cppclass
Point:
       
Point() except +
       
double getCoordinate1()
       
double getCoordinate2()
       
void setCoordinate1(double coordinate1)
       
void setCoordinate2(double coordinate2)

    cdef cppclass
SphericalPoint(Point):
       
SphericalPoint() except +
       
double getCoordinate1()
       
double getCoordinate2()
       
void setCoordinate1(double lat)
       
void setCoordinate2(double lon)

and my setup.py file -

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import
build_ext

ext_modules
= [Extension("test",
                     
["point.pyx", "Point.cpp"],
                     language
='c++',
                     
)]

setup
(
  name
= 'test',

  cmdclass
= {'build_ext': build_ext},

  ext_modules
= ext_modules
)

gives me these errors -


Error compiling Cython file:
------------------------------------------------------------
...
       
print("hello")

     
def buildPoints(self):

        cdef shared_ptr
[Point] point
        point
= shared_ptr[Point](new SphericalPoint())
                                     
^
------------------------------------------------------------

point
.pyx:14:38: 'SphericalPoint' is not a type identifier

Error compiling Cython file:
------------------------------------------------------------
...
       
print("hello")

     
def buildPoints(self):

        cdef shared_ptr
[Point] point
        point
= shared_ptr[Point](new SphericalPoint())
                                     
^
------------------------------------------------------------

point
.pyx:14:38: new operator can only be applied to a C++ class


But the same when compiled with cython --cplus point.pyx gives only a warning and no errors. Where am I going wrong 


/usr/local/lib/python3.6/dist-packages/Cython-0.29.7-py3.6-linux-x86_64.egg/Cython/Compiler/Main.py:367: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /home/winash12/test_cython/point.pxd
  tree
= Parsing.p_module(s, pxd, full_module_name)


To unsubscribe from this group and stop receiving emails from it, send an email to cython...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages