简体   繁体   中英

What's the fastest way to use C/C++ in Python?

What is the fastest way to call C/C++ functions and use C++ classes? There are various methods to do this such as Python Extension Module (Python.h), Cython, SWIG, Boost, and etc.

I've already implement C/C++ functions and C++ classes. Because performance is very important in my project.

So, I want to call C/C++ functions and use C++ classes in python with minimal modification of c/c++ code (or wrapping with no change of existing code). What is the best way?

If you already have the functions in C++, I suggest you create a dll or more and check how they can be called from python. As far as I know there is a library in python for things like this named ctypes and it works like this:

int sum(int a, int b)
{
    return a + b;
}


import ctypes
a = ctypes.WinDLL ("path\\to\\mydll.dll")

result = a.sum(10,1)

print(result)

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