ProgramVAE
PGEva
采纳率0%
2017-06-11 10:09 阅读 2.3k

使用C语言调用python

3

C语言代码
#include
#include"Python.h" //这里要包含头文件

//C/C++中执行python文件
void exec_python_file()
{
//初始化
Py_Initialize();

//choose1,执行单纯的内嵌字符串python代码,建议使用
if(!PyRun_SimpleString("execfile('draw1.py')"))
    printf("execute python file program failed\n");

//choose2,执行python文件,不建议使用
char fileStr[]="draw1.py";
FILE *fp;
if(!(fp=fopen(fileStr,"r")))
    printf("open python file failed!\n");

if(!PyRun_SimpleFile(fp,fileStr))
    printf("execute python file failed!\n");

fclose(fp);

//释放资源
Py_Finalize();

}

int main()
{
exec_python_file();
return 0;
}

 python 代码

#! /usr/bin/python python2.7
# -*- coding:utf-8 -*-

import numpy as np
import pylab as p1


x1 = [-25,-24,-23,-22,-21,-20,-19,-18,-17,-16,-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]
y1 = [20,25,38,51,103,101,95,90,102,101,235,200,181,110,132,192,381,311,273,145,404,1023,1186,1835,1875,1881,1870,1820,1190,1010,390,150,210,309,379,188,120,108,170,140,230,180,104,94,97,40,38,27,25,22,20]

p1.plot(x1,y1,'r')

p1.title('Diffraction intensity distribution')
p1.xlabel('coord/cm')
p1.ylabel('light intensity/mA')

p1.xlim(-25.0,25.0)
p1.ylim(0.0,1900.0)

p1.show()

想实现用C语言调用python
编译时报错:
gcc test.c -o test -l/usr/include/python2.7/ -lpyhton2.7



求解决

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • ProgramVAE PGEva 2017-06-11 10:10

    错误是ld: library not found for -l/usr/include/python2.7/
    clang: error: linker command failed with exit code 1 (use -v to see invocation)

    点赞 评论 复制链接分享

相关推荐