python - 对 python-dev header 中函数的 undefined reference

标签 python c gcc linker

我正在尝试将一些 Python 代码嵌入到 C 中;这是我第一次做这样的事情。

这是我第一次尝试从互联网上的指南复制的简单代码:

#include <Python.h>
void exec_pycode(const char* code)
{
  Py_Initialize();
  PyRun_SimpleString(code);
  Py_Finalize();
}

int main(int argc, char **argv) {
    exec_pycode(argv[1]);
    return 0;
}

所以我已经安装了 python3.4-dev 包。

然后为了获得我输入的链接器的信息:

pkg-config --cflags --libs python3

然后我尝试编译我的代码:

gcc -std=c99 -o main -I /usr/local/include/python3.4m  -L /usr/local/lib -lpython3.4m main.c

(按照之前的命令)

但这是结果:

/tmp/ccJFmdcr.o: in function "exec_pycode":
main.c:(.text+0xd): reference undefined to "Py_Initialize"
main.c:(.text+0x1e): reference undefined to "PyRun_SimpleStringFlags"
main.c:(.text+0x23): reference undefined to "Py_Finalize"
collect2: error: ld returned 1 exit status

链接阶段似乎存在问题,但我无法理解问题出在哪里,因为我已将 header 和库的确切路径传递给链接器。我该如何解决这个问题?

最佳答案

尝试重新排序您的编译命令,以便在您的 C 源文件之后指定所有链接选项:

gcc -std=c99 -o main -I /usr/local/include/python3.4m main.c \
  -L /usr/local/lib -lpython3.4m

关于python - 对 python-dev header 中函数的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30040373/

相关文章:

python - 云数据流流式传输,空闲时停止以省钱?

c - 检查字符串中的字符是否为 NULL 时存在缺陷

c - 使用 "Bash on Ubuntu on Windows"编译的 C 脚本是否比在 Windows 中直接编译和运行慢?

python - 绘制和提取 fft 相位

python - Elasticsearch中极限的奇怪行为

python - Blender 中的 Vim 与 BPY 一起工作?

c - 如何安排一个c函数每分钟运行一次

c - 如何检查输入何时超出二维数组列中的行?

c - 禁用 "warning: the address of ' x' 将始终评估为 'true' "

c - 如何使用链接器将目标文件转换为可执行文件(C 目标文件)