2

Let me start with a disclaimer that i'm putting this question after lot of research and not finding any direct and step by step example.

Have gone through Cython , SWIG, Boostpython documentation but couldn't get a step by step process and so posting here -

I have a .cpp & .h file with couple of mathematical functions defined.I want to call them in a python (.py) code file.

How do i integrate?Which is the best and neatest way to go about it. Please illustrate

5
  • Most people would consider this and this to be step by step guides to SWIG and Boost.Python, respectively. What exactly do you need that these don't provide? Commented Mar 3, 2015 at 12:18
  • 1
    This [post][1] seems to answer your question by using library ctypes. [1]:stackoverflow.com/questions/145270/calling-c-c-from-python Commented Mar 3, 2015 at 12:55
  • "Best" is subjective. For simple code, just make sure your cpp code has a C interface (extern "C") and use ctypes. Just look at the official docs for ctypes. Commented Mar 3, 2015 at 16:19
  • 1
    Please, add an example of one function of yours, it will become easier to propose how to do it... Commented Mar 4, 2015 at 1:10
  • Trying to do it via Cython Commented Mar 4, 2015 at 11:03

2 Answers 2

2

Since i eventually solved it using some of the help provided here and research , let me post the answer.

I did the import of CPP function via Python using Cython.

Cython wraps the Python code with CPP file and compiles the two. The outcome is a Python module (name of the module can be specified in setup.py file ) and the module can be called as usual.

The issue i was facing was calling a CPP function from Python.So here are my tips for working it out

  1. Define your function in C++ Header and CPP file.
  2. Using cdef , define the function in Python script in the beginning where you import modules define other variables.This definition is similar to C++ header file definition
  3. While pasing arguments and arrays to the function call from python , ensure all variables and arrays are type casted as CPP.
  4. For arrays, it is a bit tricky. E.G. a double array in Python can be type casted using array.array(array_to_cast). In the following example, we cast the python array, dev, into the c++ array, dev_arr. Note that dev is an array of type double in python, hence the first parameter input to array.array() is 'd'.
cdef array.array dev_arr = array.array('d',dev)   

While passing the dev_arr array to your main CPP function, you have to specify this array :data.as_doubles

fn_response = cpp_fn_call(stddev_arr.data.as_doubles,...)

Rest will work smoothly.

Sign up to request clarification or add additional context in comments.

Comments

1

See this part of the Cython documentation; it takes you step-by-step through interfacing with a C library.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.