博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c中嵌入python
阅读量:2391 次
发布时间:2019-05-10

本文共 830 字,大约阅读时间需要 2 分钟。

c编译器用DEV-C++,

python(sys.version):'2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)]'

先新建DEV-C++工程后,设置工程属性:

连接参数如下:

-IC:\Python27\include-LC:\Python27\libs-lpython27

test.py的代码:

print 'Go into the test.py!'words = raw_input('What do you want to say? ')print 'you said:'+words

main.c的代码

#include "python.h"#include 
int main(int argc, char *argv[]){ char *szPythonFileName="test.py"; Py_Initialize(); PyRun_SimpleString("print( 'PyRun_SimpleString ' )" ); PyObject *pyfile = PyFile_FromString(szPythonFileName,"r"); if(pyfile==NULL) { return 1; } FILE *f = PyFile_AsFile(pyfile); if(f==NULL) { return 1; } PyRun_AnyFileEx(f,szPythonFileName,0); Py_Finalize(); system("PAUSE" ); return 0;}

运行效果:

转载于:https://my.oschina.net/sunlimpid/blog/137043

你可能感兴趣的文章
poj1047 Round and Round We Go
查看>>
poj1222 EXTENDED LIGHTS OUT
查看>>
poj1064 Cable master
查看>>
poj2864 Pascal Library
查看>>
poj2871 A Simple Question of Chemistry
查看>>
poj2845 01000001
查看>>
poj1979 Red and Black
查看>>
poj3663 Costume Party
查看>>
poj2773 Happy 2006
查看>>
poj3904 Sky Code
查看>>
zoj 1745 Are We There Yet?
查看>>
poj 1775 && zoj 2358 Sum of Factorials
查看>>
zoj 1577 GCD & LCM
查看>>
zoj2000 Palindrome Numbers
查看>>
UVA100 The 3n + 1 problem
查看>>
hdu1754 I Hate It
查看>>
hdu 1166 敌兵布阵(求区间的和,单节点更新)
查看>>
hiho一下 第四十四周 题目1 : 博弈游戏·Nim游戏
查看>>
poj2299 Ultra-QuickSort(线段树计数问题)
查看>>
poj3264 Balanced Lineup(求区间的最大值与最小值之差)
查看>>