简体   繁体   中英

Interfacing between Python and C++

I want to interface between Python and C++. I am trying to initially do it in simplest case, eg to find mean. My main is in python

1)Function getInput (python)

2)Function CalculateMean(C++)

3)Function DisplayMean(python)

My python file (main.py) looks like this:

function getInput(x,y)
//Here I want to add the function CalculateMean written in cpp file
function displayMean(m)  

"CalcMean.h"

int CalculateMean(int x,int y)

"CalcMean.cpp"

mean = CalculateMean(x,y)    
{
mean = (x+y)/2;
return mean;
}

I have tried using SWIG, but I am a begineer and unable to solve it. Any basic help will be highly appreciated.

you need a simple swig interface file that imports your CalcMean.h and produces a module, something like this into a mymodule.i file:

%module mymodule
%{
    #include "CalcMean.h"
%}

%include "CalcMean.h"

then you can launch swig with something like

./swig -python -o mymodule.o mymodule.i

this will produce a .o file to be compiled as an .so/.dll file, as well as the required .py file to import the module.

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