From: Ondrej Certik Date: Fri, 12 Feb 2010 01:07:34 +0000 (-0800) Subject: Improve the C++ wrapping tutorial X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=eae0c4c47e708adc907f01194878406f47f83e99;p=cython.git 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 --git a/src/userguide/wrapping_CPlusPlus.rst b/src/userguide/wrapping_CPlusPlus.rst index 99db8567..73fda925 100644 --- a/src/userguide/wrapping_CPlusPlus.rst +++ b/src/userguide/wrapping_CPlusPlus.rst @@ -54,6 +54,47 @@ document. Let's assume it will be in a header file called 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 @@ to add a keyword to your Extension construction statement, as in:: 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 @@ to add a keyword to your Extension construction statement, as in:: 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