在 C 中调用 Python-3 函数

Calling Python-3 function in C

我正在尝试使用以下代码在 C 中调用 Python 代码:

#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 

但是 returns 一个错误 returns 说:

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

出了什么问题?我该如何解决这个问题?

任何帮助将不胜感激

P.S 我不确定,但这是否与我在 include 中复制了 Python 'include' 文件(包含 Python.h)有关位于 C:/MinGW

的 MinGW 文件

更新:我现在知道这样做是可以的,但被认为是不好的做法。

您没有链接到 python 库...

尝试:

gcc python_test_in_c.c -lpython3.6m

将 3.6 更改为您选择的版本...