您的位置:首页 > 运维架构 > Linux

Linux那点事

2017-01-14 19:09 190 查看
最近用到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.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 python swig c++