環境:html
關鍵詞:ubuntu 16.04 LTS;gcc 5.4;Python3.5;多版本Pythonjava
~$ 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)
實際上這個問題的發現是在一個我並不徹底掌握的環境上發現的,當時環境也是ubuntu 16.04 LTS,gcc 5.4,可是裝了anaconda3,並且還把系統默認python路徑設成了anaconda3路徑下(因此我真的很反感anaconda這個軟件,說是屏蔽系統一些配置問題,實際上出問題了就更爲難了,畢竟使用者一直屏蔽一直真香,最後出問題又沒有平時的Linux系統基礎操做經驗和基礎常識,根本無法找出來)。python
一如既往在網上找了c調用python的demo,能夠本身搜,最好參考官方文檔(這裏給出3.7的),在這裏列出一個個人demo,也是在網上找的,原連接忘了(原做者看到了可聯繫,介意的我必定刪,也能夠補上)。python方面主要是實現了兩個函數,一個是隨機返回一個指定長度的字符串,一個是返回指定範圍內的一個隨機數。linux
import random import string def mutstr(n): print("calling mutstr...(python)") ret = ''.join(random.sample(string.ascii_letters + string.digits, n)) print("python is: {0}".format(ret)) return ret def mutnum(n): print("calling mutnum...(python)") ret = random.randint(0, n) print("python is: {0}".format(ret)) return ret if __name__ == "__main__": for i in range(10): print(mutstr(i*i)) print(mutnum(i*i))
c端調用代碼以下,注意Python.h的路徑問題。ios
#include <iostream> #include <path/to/Python.h> 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也僅僅列出代碼不告訴你如何編譯,通過實驗編譯至少應當是這樣的:c++
g++ ccallpython.cpp `python3-config --cflags` `python3-config --ldflags`
若是不加`python3-config --cflags` `python3-config --ldflags`這兩個選項,編譯就會出問題,例如在個人實驗環境裏:git
~$ 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沒有一些選項:ubuntu
~$ 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版本的編譯選項不太相同:api
~$ 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編譯器也是同樣的。bash
另外網上有些說加上-I(大寫字母)和-l(小寫字母)選項的,我試了下有些選項的使用是錯誤的,即編譯器認爲是使用錯誤的,提示正確的Usage。
還有一種狀況,若是能編譯,可是運行的時候出現segment fault (core dump)頗有多是Py_Initialize();沒經過,具體能夠經過插樁式打log或者調試器發現具體出問題的地方,通常來講,這種問題也是由於版本不對的緣由。Python提供的這些C接口沒有太多的錯誤提醒(異常處理),所以出問題了要本身動手調試查找。