1

I have a bunch of functions written in python (for rapid prototyping). My main project is in C++ and I wanna call these functions from my C++ program.

These functions use some specialized python modules like numpy, pyside etc.

I tried using the "--embed" functionality of Cython to convert these functions to a C file and then compile that to an object file. Only problem is, Cython converts these functions into standalone programs, ie. makes a main() function in the .c files it creates. I don't want that, I just wanna be able to call them from my C++ program and use the results I obtain.

Are there any other alternatives, I could use to achieve this (Python/C API, SWIG etc)? Or am I using cython to embed python wrong?

I am using Python3 and Cython 0.22.1.

3
  • 1
    I think you will have to use Python/C API for this. Commented Jul 20, 2015 at 12:32
  • Doesn't that entail a lot of confusing reference decrement calls? I read that also involves a lot of data type conversion from Python to C. How will I convert numpy arrays to a type compatible in C++? Also, is there any other examples/documentation on this area apart from this? Commented Jul 20, 2015 at 12:51
  • As far as I understand the --embed cython flag embeds the python interpreter it does not allow you to interface Python code from C. For acessing numpy arrays, use the Numpy C-API. Well yes, using Python code from C is generally more work than the other way around. Commented Jul 20, 2015 at 13:00

0

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.