简体   繁体   中英

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!!!

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!

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM