C++、Qt内嵌python

网友投稿 975 2022-10-04

C++、Qt内嵌python

C++、Qt内嵌python

C++内嵌python

python重要的路径:

/usr/local/include/python3.6m/usr/local/bin/python3-config/usr/local/bin/python3.6/usr/local/bin/python3/usr/local/bin/python3.6-config/usr/local/bin/python3.6m/usr/local/bin/python3.6m-config/usr/local/lib/python3.6/usr/local/lib/python3.6/site-packages/numpy/lib/tests/data/python3.npy/usr/local/lib/pkgconfig/python3.pc/usr/local/share/man/man1/python3.1/usr/local/share/man/man1/python3.6.1/usr/share/vim/vim74/autoload/python3complete.vim/usr/share/doc/python-setuptools-0.6.10/docs/python3.txt/usr/share/doc/python-setuptools-0.6.10/docs/build/html/_sources/python3.txt

在文件​​/usr/local/lib/pkgconfig/python-3.6.pc​​中给出了引用头文件和连接库文件的信息:

# See: man pkg-configprefix=/usr/localexec_prefix=${prefix}libdir=${exec_prefix}/libincludedir=${prefix}/includeName: PythonDescription: Python libraryRequires: Version: 3.6 Libs.private: -lpthread -ldl -lutil -lrtLibs: -L${libdir} -lpython3.6mCflags: -I${includedir}/python3.6m

include路径包含了各种头文件:​​/usr/local/include/python3.6m​​​ 库文件目录为:​​​/usr/local/lib​​​ 静态库文件是​​libpython3.6m.a​​​ C/C++中内嵌python的文章:​​​:

def show(): print("hello world!")

C++代码,test.cpp

#include "Python.h"#include using namespace std;int main(int argc,char **argv){ Py_Initialize(); PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.path.append('/home/edemon/workspace/qt_cpp/untitled/tmp')"); // import py file. PyObject* pModule = PyImport_ImportModule("pytest"); if(pModule == NULL){ cout<<"my test py file open failed."; return -1; } // import function PyObject* pyFun = PyObject_GetAttrString(pModule,"show"); if(pyFun == NULL){ cout<<"pyFun get failed."; return -1; } // call function PyObject_CallFunction(pyFun,NULL); Py_Finalize(); return 0;}

使用g++编译:​​g++ test.cpp -I /usr/local/include/python3.6m -L/usr/local/lib -lpython3.6m -lrt -ldl -lutil -o test​​

C++内嵌导入其他模块的python

下面的程序是利用python安装的tushare模块请求顺丰控股的价位峰值数据

import tusharedef data_high(): print(tushare.get_hist_data(code='002352',start='2016-09-01',end='2016-09-20').high)def data_low(): print(tushare.get_hist_data(code='002352',start='2016-09-01',end='2016-09-20').low)

在C++中引用相关的函数:

#include "Python.h"#include using namespace std;int main(int argc,char **argv){ Py_Initialize(); PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.path.append('/home/edemon/workspace/qt_cpp/untitled/tmp/advanced')"); //PyRun_SimpleString("sys.path.append('/usr/local/lib/python3.6/site-packages/tushare')"); PyObject* pModule = PyImport_ImportModule("myData"); if(pModule == NULL){ cout<<"my test py file open failed."; return -1; } PyObject* pDataHigh = PyObject_GetAttrString(pModule,"data_high"); if(pDataHigh == NULL){ cout<<"data_high get failed."; return -1; } PyObject_CallFunction(pDataHigh,NULL); Py_Finalize(); return 0;}

运行:

$ g++ main.cpp -I /usr/local/include/python3.6m -L/usr/local/lib -lpython3.6m -lrt -ldl -lutil -Xlinker -export-dynamic -o main$ ./maindate2016-09-20 45.522016-09-19 42.392016-09-14 40.802016-09-13 39.702016-09-12 39.872016-09-09 40.002016-09-08 38.992016-09-07 39.402016-09-06 39.122016-09-05 39.272016-09-02 40.492016-09-01 40.36

注意加上:​​-Xlinker -export-dynamic​​,动态链接库文件。

QT内嵌python

在工程文件pro中注意includepath和libs的设置:

# add extra info for qtINCLUDEPATH += "/usr/local/include/python3.6m"LIBS += "-L/usr/local/lib" -lpython3.6m -lrt -ldl -lutil -Xlinker -export-dynamic

和C++内嵌python的例子类似,这里将得到的峰值数据写入文件中。 data.py:

#! /usr/local/bin/python3import osimport tusharedef data_high(): res = tushare.get_hist_data(code='002352',start='2016-09-01',end='2016-09-20').high os.chdir("./") res_file = open("result.txt","w") print(res,file=res_file) res_file.close()def data_low(): return ts.get_hist_data(code='002352',start='2016-09-01',end='2016-09-20').low

main.cpp:

#include "Python.h"#include #include int main(int argc,char **argv){ QApplication app(argc,argv); Py_Initialize(); PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.path.append('./')"); PyObject* pModule = PyImport_ImportModule("data"); if(pModule == NULL){ qDebug()<<"data.py can't open failed."; return -1; } PyObject* pDataHigh = PyObject_GetAttrString(pModule,"data_high"); if(pDataHigh == NULL){ qDebug()<<"data_high get failed."; return -1; } PyObject_CallFunction(pDataHigh,NULL); Py_Finalize(); return app.exec();}

你可能在make的时候会遇到类似于这样的问题:

n file included from /usr/local/include/python3.6m/pytime.h:6:0, from /usr/local/include/python3.6m/Python.h:65, from main.cpp:2:/usr/local/include/python3.6m/object.h:448:20: error: expected unqualified-id before ‘;’ token PyType_Slot *slots; /* terminated by slot==0. */

这是因为slots是qt的关键字,故在引用头文件的时候需先引用python,即​​#include "Python.h"​​​ 不能在​​#include ​​ 的后面。像这样:

#include #include "Python.h"#include

修改pro文件后,及时更新Makefile: ​​qmake -makefile​​,然后再make即可。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:小程序授权登陆的解决方法(附代码)(请先授权小程序,登录后才能操作)
下一篇:mpvue单文件页面配置的步骤介绍(mpvue文档)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~