即插即用demo系列——python 调用C++代码
2016-09-14 17:46
363 查看
python部分:pycall.py
C++部分:proc.cpp
注意,cpp代码写好后,要再编译好.so库并命名为libpyctype.so,放在和py代码同一个目录下,这样py代码才能调用cpp里的函数
另外,cpp会将函数名给改了以便支持重载,对于提供给py调用的函数,你要特别声明这个函数不要被改名,也就是extern c 。
# coding=gbk from ctypes import * clib = cdll.LoadLibrary('/home/linyc/pythonctype/libpyctype.so') print "完成装载" path = '/home/' clib.init(c_char_p(path)) print "完成init" def testpyctype(): showstr = 'test' number = 123 # 返回的就是一个c_char_p类型 retstr = create_string_buffer('\000'*100000) ret = clib.process(c_char_p(showstr), number, retstr) print 'process status = %s' % ret toprintstr = retstr.value return toprintstr if __name__ == '__main__': ret = testpyctype() print ret
C++部分:proc.cpp
#include <string> #include <iostream> using namespace std; extern "C" { void init(char * c_pt_path); int process(char * c_pt_showstr, int number, char * retstr); } // 供给python调用的初始化函数 void init(char * c_pt_path){ string tmp1 = c_pt_path; ETSPATH = tmp1; } // 给 python 调用的主函数 int process(char * c_pt_showstr, int number, char * retstr){ int state = 0; // 0表示失败 string showstr = c_pt_showstr; cout<<showstr<<endl; cout<<number<<endl; string ret = "ok"; strcpy(retstr, ret.c_str()); state = 1; return state; // 1表示成功 }
注意,cpp代码写好后,要再编译好.so库并命名为libpyctype.so,放在和py代码同一个目录下,这样py代码才能调用cpp里的函数
另外,cpp会将函数名给改了以便支持重载,对于提供给py调用的函数,你要特别声明这个函数不要被改名,也就是extern c 。
相关文章推荐
- 如何让python调用C和C++代码
- C,C++代码中调用python脚本
- 使用SWIG实现Python调用C/C++代码
- Windows C++程序调用包含TensorFlow代码的Python脚本出错问题的解决办法
- c++调用python完整代码(开发环境vs2008)
- 如何让python调用C和C++代码
- 使用swig在python中调用c++代码
- C++调用Python代码
- C++相同代码封装成库给C#调用 C++Demo和C#Demo 运行结果不一样
- linux 下python 调用C++ .so如何debug c++代码
- 通过SWIG封装C/C++代码被Python调用
- python调用 C/C++ 代码示例
- 【Cython】用Cython包装C++代码,提供给python调用
- python调用C和C++代码(BOOST)
- linux 下python 调用C++ .so如何debug c++代码
- C,C++代码中调用python脚本
- 【2013】实习收获4:关于python中利用ctypes调用c++代码的学习总结
- 利用Swig在python中调用C/C++代码
- ubuntu下C++如何调用python程序,gdb调试C++代码
- Python接口(2):使用ctpyes模块在python中调用c++代码