python 嵌入c语言,将Python嵌入C语言后,由于未定义引用`Py_Initialize',链接失败

在尝试将Python嵌入C语言并编译示例代码时,遇到链接错误,提示未定义引用`Py_SetProgramName`、`Py_Initialize`等。问题出在链接库的顺序上,需将对象文件放在链接选项之前。正确编译命令为:`gcc embedpy.o $(/usr/bin/python2.7-config --ldflags)`。
摘要由CSDN通过智能技术生成

I am trying to compile the example from the docs https://docs.python.org/2.7/extending/embedding.html and my code looks exactly like the one under 5.1:

#include

int

main(int argc, char *argv[])

{

Py_SetProgramName(argv[0]);

Py_Initialize();

PyRun_SimpleString("from time import time, ctime\n"

"print 'Today is', ctime(time())\n");

Py_Finalize();

return 0;

}

I use the following command to compile it which works fine for me and gives me the desired object file:

gcc -c $(python2.7-config --cflags) embedpy.c

To link it I use the following command which ends up in the following error:

gcc $(/usr/bin/python2.7-config --ldflags) embedpy.o

embedpy.o: In function `main':

/home/miguellissimo/embedpy.c:6: undefined reference to

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值