c编译器用DEV-C++,

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

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

python中嵌入c语言 python内嵌c语言_python中嵌入c语言

连接参数如下:

-IC:\Python27\include

-LC:\Python27\libs

-lpython27

python中嵌入c语言 python内嵌c语言_python_02

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;
}

运行效果:

python中嵌入c语言 python内嵌c语言_Go_03