This question already has an answer here:
- Calling C/C++ from Python? 14 answers
I tried with the link: Calling C/C++ from python?, but I am not able to do the same, here I have problem of declaring extern "C".so please suggest suppose I have the function called 'function.cpp' and I have to call this function in the python code. function.cpp is:
int max(int num1, int num2)
{
// local variable declaration
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
Then how i can call this function in python, as I am new for c++. I heard about the 'cython' but I have no idea about it.
Since you use C++, disable name mangling using
extern "C"
(ormax
will be exported into some weird name like_Z3maxii
):Compile it into some DLL or shared object:
now you can call it using
ctypes
: