windows 搞搞SWIG系列

SWIG,C++,lua搞一搞

  • 还是要先有example.h
#include <vector>

class Vector{
public:
    Vector(int,int);
    double abs();
    void display();
private:
    int x;
    int y;
};
  • example.cpp

#include "example.h"
#include <iostream>
#include <cmath>
using namespace std;

Vector::Vector(int a,int b){  x=a;  y=b;  }
void Vector::display(){  cout << "(" << x << ',' << y << ')' << endl; }
double Vector::abs(){    return sqrt(x*x+y*y);  }

  • 当然少不了.i文件喽

/* vector.i */
%module example
%{
#include "example.h"
%}
class Vector{
public:
    Vector(int,int);
    double abs();
    void display();
private:
    int x;
    int y;
};

全天下的儿女请注意:只有.i文件里声明的东西才可以在lua中用到,如果你将abs这个函数丢掉,那你就不可以在lua中调abs啦!!,

  • 来一个垃圾命令把!
    • swig -c++ -lua example.i
    • 这下你不不牛逼了把
    • 就产生一个example_wrap.cxx
    • 草他妈的,还得加一个#pragma comment(lib,“lua54.lib”)
    • python这里就不需要加,他真是友好啊

然后新建一个这样的工程吧!

  • 使得它产生一个example.dll

我可能已经强有力的感觉到了:你这里必须尽可能将dll的名字和你在.i里面的模块名字起得一样啊!!!

  • 写一个脚本吧
  • 把dll和脚本放在一起啊哈哈哈哈!
require "example"
a = example.Vector(12,23)
a:display()

  • 也可以开一个命令行啊
  • 牛逼死了我!

SWIG , python,C++

  • 首先你仍然很自信的拥有着

  • example.h

#include <vector>

class Vector{
public:
    Vector(int,int);
    double abs();
    void display();
private:
    int x;
    int y;
};

  • example.cpp
//vector.cpp
#include "example.h"
#include <iostream>
#include <cmath>
using namespace std;

Vector::Vector(int a,int b){  x=a;  y=b;  }
void Vector::display(){  cout << "(" << x << ',' << y << ')' << endl; }
double Vector::abs(){    return sqrt(x*x+y*y);  }

  • 你难逃宿命,需要写一个.i文件
  • example.i

注意下面的%module example,这可是直接关系到你import谁的问题!

%module example
%{
#include "example.h"
%}

class Vector{
public:
    Vector(int,int);
    double abs();
    void display();
private:
    int x;
    int y;
};
  • 让我们愉快的执行
  • swig -c++ -python -py3 example.i
    • 肯定要出现
    • example.i
    • example_wrap.cxx

  • 把这几个搞成dll把
  • 下面套路都差不多吧!

  • 关键是咋在py里面用这个C++的类呢!
import example

car = example.Vector(11,22)

car.display()
  • 我可真牛逼

windows,swig,python,VS工程搞一个在py中调用C函数

  • 首先你要有一个C文件
 /* File : example.c */
 
#include <time.h>
              
#pragma warning(disable:4996)

double My_variable = 3.0;
 
 int fact(int n) {
     if (n <= 1) return 1;
     else return n*fact(n-1);
 }
 
 int my_mod(int x, int y) {
     return (x%y);
 }
 	
 char *get_time()
 {
     time_t ltime;
     time(&ltime);
     return ctime(&ltime);
 }

  • 然后你需要有一个.i文件
/* example.i */
 %module example
 %{
 /* Put header files here or function declarations like below */
 extern double My_variable;
 extern int fact(int n);
 extern int my_mod(int x, int y);
 extern char *get_time();
 %}
 
 extern double My_variable;
 extern int fact(int n);
 extern int my_mod(int x, int y);
 extern char *get_time();

  • 请执行
  • swig -python example.i
  • 这下生成1个狗东西
  • example_wrap.c
  • example.py

  • 新建一个VS工程

  • 工程里需要有example.c

  • example_wrap.c

  • 记得加入python的头文件和lib库的位置哦

还有就是要把工程的配置和平台设置成你的python版本一样!

  • 生成工程
  • 出来一个dll
  • 重命名为_example.pyd

  • 写一个A.py

import example

print(example.fact(5))

  • 把_example.pyd和A.py同一个目录把!
  • 打开
  • 你也太牛了把!

  • 或者再当前目录下进入交互窗口
    • 也行!

windows下安装SWIG

  • 地址
  • https://sourceforge.net/projects/swig/files/

  • 我进入swigwin,下载了swigwin-4.0.1.zip
  • 解压之后这个样子

  • 然后直接配置环境变量就好了
  • 根本不需要安装

  • 还可以输入swig --help看看成功了没有
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fgh431

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值