Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So after lots of poking, trial and error, screaming and tearing my hair out, I finally got this to work. First though, I had to re-write my C++ into C, which for me really just involved converting all my <code>std::string</code> variables to <code>char*</code> and keeping track of some lengths.</p> <p>Once done I had my .h and .c files. I wanted to make a single function from the C code available in Python. It turns out that Cython can compile your C files into the extension for you and link any libraries all in one go, so starting with my setup.py, it ended up looking like this:</p> <pre><code>from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext ext_modules=[ Extension("myext", ["myext.pyx", "../stuff.c"], libraries=["ssl", "crypto"] ) ] setup( name = "myext", cmdclass = {"build_ext": build_ext}, ext_modules = ext_modules ) </code></pre> <p>As you can see, the second argument to the Extension simply lists all the files that need to be compiled, Cython works out how to compile them depending on their file extension as far as I can tell. The libraries array tells the Cython compiler what needs to be linked in (in this case I was wrapping some crypto stuff that I couldn't seem to mimick directly through existing Python libs).</p> <p>To actually make my C function available in the .pyx file, you write a small wrapper in the .pxd. My myext.pxd looked as below:</p> <pre><code>cdef extern from "../stuff.h": char* myfunc(char* arg1, char* arg2, char* arg3) </code></pre> <p>In the .pyx you then use the cimport declaration to import this function, which is then available for use as if it were any other Python function:</p> <pre><code>cimport myext def my_python_func(arg1, arg2, arg3): result = myext.myfunc(arg1, arg2, arg3) return result </code></pre> <p>When you build this (on Mac at least) you get a .so that you can import in python and run the functions from the .pyx. There may be better, more correct way to get this all working but that comes from experience and this was a first encounter that I managed to work out. I'd be very interested on pointers where I may have gone wrong.</p> <p><strong>Update:</strong></p> <p>After further use of Cython, I found it was super simple to integrate it with C++ too, once you know what you're doing. Making C++'s <code>string</code> available is as simple as <code>from libcpp.string cimport string</code> in your pyx/pyd. Declaring the C++ class is similarly easy as:</p> <pre><code>cdef extern from "MyCPPClass.h": cdef cppclass MyCPPClass: int foo; string bar; </code></pre> <p>Sure you have to basically redeclare the .h definition of your class in a Pythonic format, but that's a small price to pay for getting access to your already written C++ functions.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload