用C代码调用python函数 [英] Call python function in C code

查看:69
本文介绍了用C代码调用python函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中写了一些小代码,例如两个数字的加法。

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

如何在C(mainpgm)程序中调用add.py/add函数。

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.

任何建议在c中调用该函数。

Any suggestions to call the function in c.

推荐答案

阅读 Python手册

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

这篇关于用C代码调用python函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆