我试图用这个代码来调用Python代码在C:调用的Python 3功能用C
#include <stdio.h>
#include <Python.h>
int main()
{
PyObject* pInt;
Py_Initialize();
PyRun_SimpleString("print('This is Python in C')");
Py_Finalize();
printf("\n");
return 0;
}
,并尝试使用下面的命令编译它:
gcc python_test_in_c.c
但是它返回一个错误返回说:
undefined referance to `__imp __Py_Initialise`
undefined referance to `__imp__PyRun_SimpleSringFlags`
undefined referance to `__imp__Py_Finalise`
collect2.exe: error: ld returned 1 exit status
怎么回事?我怎样才能解决这个问题?
任何帮助,将不胜感激
PS我不知道,但是这会是什么做我复制了Python的事实“包括”文件中位于对MinGW的包含文件(含Python.h)在C:/ MinGW
更新:我现在知道这是可以做,但认为不好的做法。
你没有链接库。 – litelite
请编辑标题!它没有任何意义,只是误导。你不用“在C中嵌入Python代码”。您只需调用Python C API中的函数即可。这与其他图书馆没有什么不同。 – Olaf
@Olaf在技术上,这看起来像嵌入到我看到'PyRun_SimpleString(“print('这是C中的Python)”);'。它是通过库API嵌入的。 –