Windows下C语言中Python的嵌入

2024-09-29 22:27:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在Windows10上使用minGW编译一个包含Python.h头的.c文件。我的目标是在C语言中嵌入一些pythoncode,这只是编译的一个小测试。
我的源文件如下所示:

#include <stdio.h>
#include <C:\Python27\include\Python.h>

int main() 
{
    printf("Hello World from C");

    Py_Initialize();

    PyRun_SimpleString("print('Hello World from Python!!!')");

    Py_Finalize();


    return 0; 
}


我的makefile如下所示:

all: HelloWorld.c
    gcc -Wall -I C:\Python27\include -L C:\Python27\libs  -o HelloWorld.exe HelloWorld.c -lpython27


因此,如果我用这个makefile编译源代码,我会得到以下错误:

gcc -Wall -I C:\Python27\include -L C:\Python27\libs  -o HelloWorld.exe HelloWorld.c -lpython27
C:\Users\ga96dux\AppData\Local\Temp\ccUQs2pM.o:HelloWorld.c:(.text+0x1b): undefined reference to `_imp__Py_Initialize'
C:\Users\ga96dux\AppData\Local\Temp\ccUQs2pM.o:HelloWorld.c:(.text+0x31): undefined reference to `_imp__PyRun_SimpleStringFlags'
C:\Users\ga96dux\AppData\Local\Temp\ccUQs2pM.o:HelloWorld.c:(.text+0x38): undefined reference to `_imp__Py_Finalize'
collect2.exe: error: ld returned 1 exit status
make: *** [all] Error 1


我已经尝试了所有我能在互联网上找到的东西,但它不起作用,makefile有什么问题吗,有什么建议如何解决这个问题?非常感谢你!你知道吗


Tags: textpyincludelocalexeusersappdatatemp

热门问题