一个经常出现的问题是:如何从我的C程序中调用Python? 有很多方法,所以让我们回顾其中的三种。
系统方法我们可以使用系统函数从C程序调用Python,这基本上意味着,我们确实需要一个已编写的python程序,或者您需要使C程序将python程序写入磁盘。 这是一个简单的示例:
#include <stdio.h>
int main(){
char *prg = (char*)"print \"hello from python\"\n";
FILE *f= (FILE*)fopen("test2.py","w");
fprintf(f, prg);
fclose(f);
system("python test2.py");
}
假设程序已保存为test