无法通过C调用Python引用Py_Initialize

1 投票
1 回答
892 浏览
提问于 2025-04-17 17:34

我想用一个C程序去调用一个Python程序。

操作系统:ubuntu 12.10 x64

Python版本:2.7.3

C语言代码:

#include <stdio.h>
#include <stdlib.h>
#include <python2.7/Python.h>

int main(int argc, char** argv)
{
    printf("Hello world!\n");
    Py_Initialize();
    Py_SetProgramName("c_python");
    PyRun_SimpleString("print \"Hello world,Python!\"\n");
    Py_Finalize();
    exit(0);
}

编译命令:

gcc -I/usr/include/python2.7 -L/usr/lib/python2.7 -Wall -fPIC c_python.c -o c_pyton


/tmp/cciuHgrf.o:in ‘main’:
c_python.c:(.text+0x1c):reference undefined ‘Py_Initialize’
c_python.c:(.text+0x28):reference undefined ‘Py_SetProgramName’
c_python.c:(.text+0x3e):reference undefined ‘Py_Finalize’
collect2: error: ld return 1

1 个回答

2

你需要把Python解释器链接到你的可执行文件里:-lpython

撰写回答