Linux那点事

来源:互联网 发布:origin怎么导入数据 编辑:程序博客网 时间:2024/06/02 05:32

最近用到Linux比较多 所以需要恶补一下 。。。。

说一下最近的工作内容吧 ,很简单  使用swig 封装C++函数 生成供python调用的模块 就这些。。。

swig  说白了就是一个封装的工具  它会根据写好的c/c++ 的函数代码生成一个封装代码  然后 将封装代码 与  c/c++ 函数代码共同编译链接 最后生成供python调用的模块(其实我看来就是给原来的c/c++函数套了一个python认识的面具而已)

我用了以下几步:

  1.  swig -python -c++  animal.i   生成了 hello_wrap.cxx  和 hello.py
  2. g++ -fpic -c animal_wrap.cxx   animal.cpp 生成了 animal_wrap.o animal.o
  3. 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 2Dog's mouth has 1Dog's nose has 1

这就是最基本的swig 封装 以及python调用



1 0