Linux那点事
2017-01-14 19:09
253 查看
最近用到Linux比较多 所以需要恶补一下 。。。。
说一下最近的工作内容吧 ,很简单 使用swig 封装C++函数 生成供python调用的模块 就这些。。。
swig 说白了就是一个封装的工具 它会根据写好的c/c++ 的函数代码生成一个封装代码 然后 将封装代码 与 c/c++ 函数代码共同编译链接 最后生成供python调用的模块(其实我看来就是给原来的c/c++函数套了一个python认识的面具而已)
我用了以下几步:
swig -python -c++ animal.i 生成了 hello_wrap.cxx 和 hello.py
g++ -fpic -c animal_wrap.cxx animal.cpp 生成了 animal_wrap.o animal.o
g++ -shared animal_wrap.o animal.o -o _animal.so 生成了 _animal.so
1 /*file:animal.h*/
2 int func();
1 /*file:animal.i*/
2 %module animal
3 %{
4 #include "animal.h"
5 %}
6 int func();
这就是最基本的swig 封装 以及python调用
说一下最近的工作内容吧 ,很简单 使用swig 封装C++函数 生成供python调用的模块 就这些。。。
swig 说白了就是一个封装的工具 它会根据写好的c/c++ 的函数代码生成一个封装代码 然后 将封装代码 与 c/c++ 函数代码共同编译链接 最后生成供python调用的模块(其实我看来就是给原来的c/c++函数套了一个python认识的面具而已)
我用了以下几步:
swig -python -c++ animal.i 生成了 hello_wrap.cxx 和 hello.py
g++ -fpic -c animal_wrap.cxx animal.cpp 生成了 animal_wrap.o animal.o
g++ -shared animal_wrap.o animal.o -o _animal.so 生成了 _animal.so
1 /*file:animal.cpp*/ 2 #include<iostream> 3 using namespace std; 4 class Animal{ 5 public: 6 int eyes; 7 int mouth; 8 int nose; 9 }; 10 int func(){ 11 Animal Dog; 12 Dog.eyes = 2; 13 Dog.mouth = 1; 14 Dog.nose = 1; 15 cout << "Dog's eyes have " << Dog.eyes << endl; 16 cout << "Dog's mouth has " << Dog.mouth << endl; 17 cout << "Dog's nose has " << Dog.nose << endl; 18 return 0; 19 }
1 /*file:animal.h*/
2 int func();
1 /*file:animal.i*/
2 %module animal
3 %{
4 #include "animal.h"
5 %}
6 int func();
1 ##file:test.py 2 import animal 3 a=animal.func() 4 print a
[boy@localhost src]$ python test.py Dog's eyes have 2 Dog's mouth has 1 Dog's nose has 1
这就是最基本的swig 封装 以及python调用
相关文章推荐
- Linux socket 初步
- 使用C++实现JNI接口需要注意的事项
- Linux Kernel 4.0 RC5 发布!
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- linux lsof详解
- linux 文件权限
- Linux 执行数学运算
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- 10 篇对初学者和专家都有用的 Linux 命令教程
- Linux 与 Windows 对UNICODE 的处理方式
- Ubuntu12.04下QQ完美走起啊!走起啊!有木有啊!
- 解決Linux下Android开发真机调试设备不被识别问题