C语言字符的引用是用AS,在C中嵌入Python:将未定义的引用链接到PyString_AsString时出错...

我试图在C程序中嵌入一个python程序。我的操作系统是ubuntu14.04

我尝试在同一个C代码库中嵌入Python2.7和Python3.4解释器(作为单独的应用程序)。编译和链接在嵌入Python2.7时起作用,但对于Python3.4则不行。它在链接器阶段失败。在

这是我的C代码(只是一个示例,不是真正的代码)

simple.c#include

#include

int main(int argc, char *argv[])

{

PyObject *pName, *pModule, *pFunc, *pValue;

char module[] = "get_version";

char func[] = "get_version";

char module_path[] = ".";

Py_Initialize();

PyObject *sys_path = PySys_GetObject("path");

PyList_append(sys_path, PyUnicode_FromString(module_path));

pName = PyUnicode_FromString(module);

pModule = PyImport_Import(pName);

Py_DECREF(pName);

if(pModule != NULL)

{

pFunc = PyObject_GetAttrString(pModule, func);

if (pFunc && PyCallable_Check(pFunc))

{

pValue = PyObject_CallObject(pFunc, NULL);

if (pValue != NULL) {

printf("Python version: %s\n", PyString_AsString(pValue));

Py_DECREF(pValue);

}

else {

Py_DECREF(pFunc);

Py_DECREF(pModule);

PyErr_Print();

fprintf(stderr,"Call failed\n");

return 1;

}

}

}

Py_Finalize();

return 0;

}

得到_版本.py

^{pr2}$

我用gcc编译程序。首先,将compilating and linking flags设置为python 2.7,我使用以下命令运行编译和链接:gcc `python-config --cflags` simple.c `python-config --ldflags`

标志扩展为:

python配置--cflags:-I/usr/include/python2.7 -I/usr/include/x86_64-linux-gnu/python2.7 -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes

python配置--ldflags:-L/usr/lib/python2.7/config-x86_64-linux-gnu -L/usr/lib -lpthread -ldl -lutil -lm -lpython2.7 -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions

它工作正常,没有任何问题。当我试图用python3.4标志编译它时,它失败了:gcc `python3-config --cflags` simple.c `python3-config --ldflags`

标志扩展为:

python配置--cflags:-I/usr/include/python3.4m -I/usr/include/python3.4m -Wno-unused-result -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes

python配置--ldflags:-L/usr/lib/python3.4/config-3.4m-x86_64-linux-gnu -L/usr/lib -lpython3.4m -lpthread -ldl -lutil -lm -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions

错误消息:simple.c: In function ‘main’:

simple.c:27:17: warning: implicit declaration of function ‘PyString_AsString’ [-Wimplicit-function-declaration]

printf("Python version: %s\n", PyString_AsString(pValue));

^

simple.c:27:17: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]

/tmp/ccaoMdTo.o: In function `main':

/home/vagrant/c_python_api/simple.c:27: undefined reference to `PyString_AsString'

collect2: error: ld returned 1 exit status

我试图改变链接器对象的指定顺序。但运气不好。你知道为什么会这样吗?在

谢谢你的帮助!!在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值