Hi, we made some improvements into the C++ wrapping tutorial, patch attached, I've also pushed it here:
http://bitbucket.org/certik/cython-doc/ so that you can just pull it. Essentially, my colleague followed the tutorial step by step and he got missing symbols when he imported the module in Python. So we improved the tutorial, so that it is explicit now. See the log in the patch (or better the patch itself) for the details. Thanks, Ondrej & Aayush
# HG changeset patch # User Ondrej Certik <[email protected]> # Date 1265936854 28800 # Node ID 0ac811783dd0505bdd40bf1bdb18c2af8f1548d6 # Parent e447286543891762be66174a3659f7ddb09645ec Improve the C++ wrapping tutorial Previously it didn't work if followed step by step (one has to link the Rectangle.cpp file). This patch makes everything clear and explicit. diff -r e44728654389 -r 0ac811783dd0 src/userguide/wrapping_CPlusPlus.rst --- a/src/userguide/wrapping_CPlusPlus.rst Thu Dec 10 11:07:44 2009 -0800 +++ b/src/userguide/wrapping_CPlusPlus.rst Thu Feb 11 17:07:34 2010 -0800 @@ -54,6 +54,47 @@ void move(int dx, int dy); }; +and the implementation in the file called :file:`Rectangle.cpp`: + +.. sourcecode:: c++ + + #include "Rectangle.h" + + Rectangle::Rectangle(int X0, int Y0, int X1, int Y1) + { + x0 = X0; + y0 = Y0; + x1 = X1; + y1 = Y1; + } + + Rectangle::~Rectangle() + { + } + + int Rectangle::getLength() + { + return (x1 - x0); + } + + int Rectangle::getHeight() + { + return (y1 - y0); + } + + int Rectangle::getArea() + { + return (x1 - x0) * (y1 - y0); + } + + void Rectangle::move(int dx, int dy) + { + x0 += dx; + y0 += dy; + x1 += dx; + y1 += dy; + } + This is pretty dumb, but should suffice to demonstrate the steps involved. Specify C++ language in setup.py @@ -65,7 +106,7 @@ ext = Extension( "rectangle", # name of extension - ["rectangle.pyx"], # filename of our Cython source + ["rectangle.pyx", "Rectangle.cpp"], # filename of our Cython source language="c++", # this causes Cython to create C++ source include_dirs=[...], # usual stuff libraries=[...], # ditto @@ -73,6 +114,13 @@ cmdclass = {'build_ext': build_ext} ) +and Cython will generate and compile the :file:`rectangle.cpp` file (from the +:file:`rectangle.pyx`), then it will compile :file:`Rectangle.cpp` +(implementation of the ``Rectangle`` class) and link both objects files +together into :file:`rectangle.so`, which you can then import in Python using +``import rectangle`` (if you forget to link the :file:`Rectangle.o`, you will +get missing symbols while importing the library in Python). + With the language="c++" keyword, Cython distutils will generate a C++ file. Create cdef extern from block
_______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
