云锦诚科技专注四川德阳网站设计 四川网站制作 四川网站建设
四川德阳网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

创新互联Python教程:python怎么在c中运行程序

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

网页题目:创新互联Python教程:python怎么在c中运行程序
标题链接:http://xiwangwangguoyuan.com/article/dhjhihs.html

其他资讯