Haider
Haider

Reputation: 341

Call C/C++ code from Python

I am currently working on a project to import a Matlab program to python for integration into ImageJ as a plugin. The program contains Mex files and the source code was written in C++. Is there a way to call the C++ functions without having to rewrite them in python. Thanks!!!

Upvotes: 0

Views: 1453

Answers (2)

nneonneo
nneonneo

Reputation: 179422

If you can build your program as a shared library, then you can use the ctypes foreign-function interface to call your functions.

This is often less work (and less complex) than wrapping the functions with Cython or writing your own C-API extension, but it is also more limited in what you can do. Therefore, I recommend starting with ctypes, and moving up to Cython if you find that ctypes doesn't suit your needs.

However, for simple libraries, ctypes will do just fine (I use it a lot).

Upvotes: 1

Noob Saibot
Noob Saibot

Reputation: 4749

Welcome to Stack Overflow!

You would need to wrap those C functions into python C extensions, through the Python C-API, for them to be callable from python; however, i can tell you from experience that the task can be a headache if you don't know what you're doing.

I would recommend checking out cython. It makes writing C extensions as easy as writing python code!

Upvotes: 1

Related Questions