Call python function in C code [closed]

2019-07-12 12:53发布

i written small code like addition for two numbers in python.

addition.py
  def add(a,b):
       c=a+b
       print "result is...",c


mainpgm.c
 ???

how to call the addition.py/add function in the C(mainpgm) program.

Already i saw some links but i am not to getting clear.

Any suggestions to call the function in c.

标签: python c cython
1条回答
来,给爷笑一个
2楼-- · 2019-07-12 13:18

Reading the Python manual,

#include <Python.h>

int main(int argc, char *argv[])
{
  Py_SetProgramName(argv[0]);  /* optional but recommended */
  Py_Initialize();
  PyRun_SimpleString("import addition\naddition.add(42, 42)\n");
  Py_Finalize();
  return 0;
}

might do what you want.

查看更多
登录 后发表回答