C语言中运行python程序
C语言使用popen/system或者直接以系统调用级fork+exec来运行python程序也是一种混编的手段了。
举例如下,Python代码如下
#!/usr/bin/env python # test.py import sys x = int(sys.argv[1]) print x*x
C语言代码如下
/* test.c */ #include <stdio.h> #include <stdlib.h> int main() { FILE *f; char s[1024]; int ret; f = popen("./test.py 99", "r"); while((ret=fread(s,1,1024,f))>0) { fwrite(s,1,ret,stdout); } fclose(f); return 0; }
测试如下
$ gcc test.c $ ./a.out 9801
Copyright © 2019- bangwoyixia.com 版权所有 湘ICP备2023022004号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务