Linux那点事


最近用到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 2
Dog's mouth has 1
Dog's nose has 1

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



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值