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

环境:
关键词:ubuntu 16.04 LTS;gcc 5.4;Python3.5;多版本Python
c/c++调用python的一些坑,主要是编译问题和版本问题
文章图片
c/c++调用python的一些坑,主要是编译问题和版本问题
文章图片

~$ cat /etc/issue Ubuntu 16.04.6 LTS \n \l~$ cat /proc/version Linux version 4.15.0-50-generic (buildd@lgw01-amd64-029) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.10)) #54~16.04.1-Ubuntu SMP Wed May 8 15:55:19 UTC 2019~$ g++ -v Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.11' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.11)

View Code
实际上这个问题的发现是在一个我并不完全掌握的环境上发现的,当时环境也是ubuntu 16.04 LTS,gcc 5.4,但是装了anaconda3,而且还把系统默认python路径设成了anaconda3路径下(所以我真的很反感anaconda这个软件,说是屏蔽系统一些配置问题,实际上出问题了就更为难了,毕竟使用者一直屏蔽一直真香,最后出问题又没有平时的Linux系统基础操作经验和基础常识,根本没法找出来)。

一如既往在网上找了c调用python的demo,可以自己搜,最好参考官方文档(这里给出3.7的),在这里列出一个我的demo,也是在网上找的,原链接忘了(原作者看到了可联系,介意的我一定删,也可以补上)。python方面主要是实现了两个函数,一个是随机返回一个指定长度的字符串,一个是返回指定范围内的一个随机数。
import random import stringdef mutstr(n): print("calling mutstr...(python)") ret = ''.join(random.sample(string.ascii_letters + string.digits, n)) print("python is: {0}".format(ret)) return retdef mutnum(n): print("calling mutnum...(python)") ret = random.randint(0, n) print("python is: {0}".format(ret)) return retif __name__ == "__main__": for i in range(10): print(mutstr(i*i)) print(mutnum(i*i))


c端调用代码如下,注意Python.h的路径问题。
#include #include int main() { // 初始化,载入python的扩展模块 Py_Initialize(); if(!Py_IsInitialized()) { std::cout << "Python init failed!" << std::endl; return -1; }// PyRun_SimpleString 为宏,执行一段python代码 // 导入当前路径 PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.path.append('./')"); PyRun_SimpleString("import random"); PyRun_SimpleString("import string"); PyRun_SimpleString("print(''.join(random.sample(string.ascii_letters + string.digits, 10)))"); PyObject *pModule = NULL; PyObject *pDict = NULL; PyObject *pFunc = NULL; PyObject *pArgs = NULL; PyObject *pRet = NULL; // 使用PyObject* pModule来存储导入的.py文件模块 pModule = PyImport_ImportModule("python2c"); if(!pModule) { std::cout << "Load python2c.py failed!" << std::endl; return -1; }// 使用PyObject* pDict来存储导入模块中的方法字典 pDict = PyModule_GetDict(pModule); if(!pDict) { std::cout << "Can't find dict in python2c!" << std::endl; return -1; }// 获取方法 pFunc = PyDict_GetItemString(pDict, "mutstr"); if(!pFunc || !PyCallable_Check(pFunc)) { std::cout << "Can't find function!" << std::endl; return -1; }/* 向Python传参数是以元组(tuple)的方式传过去的, 因此我们实际上就是构造一个合适的Python元组就 可以了,要用到PyTuple_New,Py_BuildValue,PyTuple_SetItem等几个函数 */ pArgs = PyTuple_New(1); //PyObject* Py_BuildValue(char *format, ...) //把C++的变量转换成一个Python对象。当需要从 //C++传递变量到Python时,就会使用这个函数。此函数 //有点类似C的printf,但格式不同。常用的格式有 //s 表示字符串, //i 表示整型变量, 如Py_BuildValue("ii",123,456) //f 表示浮点数, //O 表示一个Python对象 PyTuple_SetItem(pArgs, 0, Py_BuildValue("i", 60)); // 调用python的mutstr函数 pRet = PyObject_CallObject(pFunc, pArgs); if (pRet != NULL) { char* retstr = NULL; PyArg_Parse(pRet, "s", &retstr); std::cout << "-- c++ is: " << retstr << std::endl; }// 调用python的mutnum函数 pFunc = PyDict_GetItemString(pDict, "mutnum"); if(!pFunc || !PyCallable_Check(pFunc)) { std::cout << "Can't find function!" << std::endl; return -1; } pRet = PyObject_CallObject(pFunc, pArgs); if (pRet != NULL) { int retnum = 0; PyArg_Parse(pRet, "i", &retnum); std::cout << "-- c++ is: " << retnum << std::endl; }// 清理python对象 if(pArgs) { Py_DECREF(pArgs); } if(pModule) { Py_DECREF(pModule); }//关闭python调用 Py_Finalize(); return 0; }


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

如果不加`python3-config --cflags` `python3-config --ldflags`这两个选项,编译就会出问题,例如在我的实验环境里:
c/c++调用python的一些坑,主要是编译问题和版本问题
文章图片
c/c++调用python的一些坑,主要是编译问题和版本问题
文章图片
~$ 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

View Code
然而,如果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'

【c/c++调用python的一些坑,主要是编译问题和版本问题】实际上,有一些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接口没有太多的错误提醒(异常处理),因此出问题了要自己动手调试查找。

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

    推荐阅读