【c++】——c/c++调用python的一些坑,主要是编译问题和版本问题

60 篇文章 0 订阅

转载自:https://www.cnblogs.com/LittleSec/p/10940758.html

注意了,我在官方文档中也没找到编译的命令,很多网上的demo也仅仅列出代码不告诉你如何编译,经过实验编译至少应当是这样的:

g++ ccallpython.cpp `python3-config --cflags` `python3-config --ldflags`

如果不加python3-config --cflags python3-config --ldflags这两个选项,编译就会出问题,例如在我的实验环境里:

~$ g++ ccallpython.cpp 
/tmp/ccOuCjpu.o: In function `main':
ccallpython.cpp:(.text+0x19): undefined reference to `Py_Initialize'
ccallpython.cpp:(.text+0x1e): undefined reference to `Py_IsInitialized'
ccallpython.cpp:(.text+0x5c): undefined reference to `PyRun_SimpleStringFlags'
ccallpython.cpp:(.text+0x6b): undefined reference to `PyRun_SimpleStringFlags'
ccallpython.cpp:(.text+0x7a): undefined reference to `PyRun_SimpleStringFlags'
ccallpython.cpp:(.text+0x89): undefined reference to `PyRun_SimpleStringFlags'
ccallpython.cpp:(.text+0x98): undefined reference to `PyRun_SimpleStringFlags'
ccallpython.cpp:(.text+0xca): undefined reference to `PyImport_ImportModule'
ccallpython.cpp:(.text+0x107): undefined reference to `PyModule_GetDict'
ccallpython.cpp:(.text+0x149): undefined reference to `PyDict_GetItemString'
ccallpython.cpp:(.text+0x160): undefined reference to `PyCallable_Check'
ccallpython.cpp:(.text+0x1a4): undefined reference to `PyTuple_New'
ccallpython.cpp:(.text+0x1bc): undefined reference to `Py_BuildValue'
ccallpython.cpp:(.text+0x1d0): undefined reference to `PyTuple_SetItem'
ccallpython.cpp:(.text+0x1e3): undefined reference to `PyObject_CallObject'
ccallpython.cpp:(.text+0x210): undefined reference to `PyArg_Parse'
ccallpython.cpp:(.text+0x24c): undefined reference to `PyDict_GetItemString'
ccallpython.cpp:(.text+0x263): undefined reference to `PyCallable_Check'
ccallpython.cpp:(.text+0x2b0): undefined reference to `PyObject_CallObject'
ccallpython.cpp:(.text+0x2dc): undefined reference to `PyArg_Parse'
ccallpython.cpp:(.text+0x398): undefined reference to `Py_Finalize'
collect2: error: ld returned 1 exit status

然而,如果python版本和gcc版本不对的话,还会出现别的问题,以python3.7为例,会提示gcc没有一些选项:

~$ g++ ccallpython.cpp `python3-config --cflags` `python3-config --ldflags`
g++: error: unrecognized command line option ‘-fno-plt’
error: unknown argument: '-flto-partition=none'

实际上,有一些bash使用经验的人都知道,xxx里的xxx会当成命令来执行,因此在终端里输入python3-config --cflags就能看到输出,实际上一些编译选项,不同python版本的编译选项不太相同:

~$ python3.7-config --cflags
-I/home/xxx/anaconda3/include/python3.7m -I/home/xxx/anaconda3/include/python3.7m  -Wno-unused-result -Wsign-compare -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O3 -pipe  -fdebug-prefix-map==/usr/local/src/conda/- -fdebug-prefix-map==/usr/local/src/conda-prefix -fuse-linker-plugin -ffat-lto-objects -flto-partition=none -flto -flto -fuse-linker-plugin -ffat-lto-objects -flto-partition=none -DNDEBUG -fwrapv -O3 -Wall

~$ python3.5-config --cflags
-I/usr/include/python3.5m -I/usr/include/python3.5m  -Wno-unused-result -Wsign-compare -g -fstack-protector-strong -Wformat -Werror=format-security  -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes

当你搜索错误后就会大体知道,解决方法都是升级gcc版本,升级到gcc7,选项-fno-plt等选项是更新的gcc版本才支持的。对于clang编译器也是一样的。

另外网上有些说加上-I(大写字母)和-l(小写字母)选项的,我试了下有些选项的使用是错误的,即编译器认为是使用错误的,提示正确的Usage。

还有一种情况,如果能编译,但是运行的时候出现segment fault (core dump)很有可能是Py_Initialize();没通过,具体可以通过插桩式打log或者调试器发现具体出问题的地方,一般来说,这种问题也是因为版本不对的原因。Python提供的这些C接口没有太多的错误提醒(异常处理),因此出问题了要自己动手调试查找。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值