g++编译加入-fPIC -shared参数后调用PyRun_SampleString段错误,,为了实现抓包功能,用c+
g++编译加入-fPIC -shared参数后调用PyRun_SampleString段错误,,为了实现抓包功能,用c+
为了实现抓包功能,用c++写了个so库供python调用,编译命令为
g++ -o sniff.so -fPIC -shared sniff.cpp -lsqlite3 -lpcap -I/usr/include/python2.7 -L/usr/lib/python2.7 -lpython2.7
但是发现加入-fPIC -shared参数后所有Python.h中的函数,如PyRunSampleString等执行的时候都会出现段错误。
用以下例子做示范:
//abc.py C++调用这个模块print "hello"
//a.cpp 编译为可供调用的库#include <Python.h>#include "string"#include "iostream"#include <stdio.h>using namespace std;int main(){ cout<<"going init python\n"; Py_Initialize(); cout<<"init python success\n"; if (!Py_IsInitialized() ){ cerr<<"Python init failed!"<<endl; return -1; } PyObject *moduleName,*pModule,*pClass,*pEvent,*pFire,*pArgs; cout<<"python init success!\n"; PyRun_SimpleString("import sys"); cout<<"import sys\n"; PyRun_SimpleString("sys.path.append('./')"); cout<<"sys path append\n"; pModule = PyImport_ImportModule("abc"); if(!pModule){ cerr<<"sniff.so:can not load events.py"<<endl; return -1; } cout<<"load python module success\n"; Py_Finalize(); return 0;}
编译指令:g++ -o a -fPIC -shared a.cpp -I/usr/include/python2.7 -L/usr/lib/python2.7 -lpython2.7 执行时段错误
编译指令:g++ -o a a.cpp -I/usr/include/python2.7 -L/usr/lib/python2.7 -lpython2.7 正确执行
不加 -fPIC -shared 参数执行是没问题的,但是这个c++库文件就是用来供python调用的,不能不加。。。
请各位大神看有没有解决办法,万谢
编橙之家文章,
相关内容
- 如何在Python中使用subprocess准备shell执行环境,pythonsub
- Flask 中 url for 不理解是什么意思.甚至不理解其作用,
- flask-sqlalchemy 如何查询关系表中的一个字段?,,class
- 如何在mac上用python批量将word文件转成txt文件,pythontx
- python 字符问题'utf8' codec can't decode byte 0xe6 in position 0,
- peewee postgres ArrayField 和JSONField怎么update数据,peeweepos
- 线上环境部署Django,线上部署django,请问大家是如何在线
- windows下python控制台输出中文乱码问题?,,在做爬虫的
- web2py db.define_table(),,中的format关键字参
- 自己写的Flask mysql操作类出错?,flaskmysql,操作类如下#
评论关闭