用SWIG从包装的cpp文件创建一个DLL [英] Creating a DLL from a wrapped cpp file with SWIG

查看:132
本文介绍了用SWIG从包装的cpp文件创建一个DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何在Windows上使用SWIG。



以下是我的c ++代码:

  / *文件: example.cxx * / 

#includeexample.h
#define M_PI 3.14159265358979323846

/ *将形状移动到新位置* /
void Shape :: move(double dx,double dy){
x + = dx;
y + = dy;
}

int Shape :: nshapes = 0;

double Circle :: area(void){
return M_PI * radius * radius;
}

double Circle :: spaces(void){
return 2 * M_PI * radius;
}

double Square :: area(void){
return width * width;
}

double Square :: circles(void){
return 4 * width;
}

这是我的头文件:

  / *文件:example.h * / 

类Shape {
public:
Shape(){
nshapes ++;
}
virtual〜Shape(){
nshapes--;
};
double x,y;
void move(double dx,double dy);
虚拟双面(void)= 0;
虚拟双边界(void)= 0;
static int nshapes;
};

class Circle:public Shape {
private:
double radius;
public:
Circle(double r):radius(r){};
虚拟双面(void);
虚拟双周界(void);
};

class Square:public Shape {
private:
double width;
public:
Square(double w):width(w){};
虚拟双面(void);
虚拟双周界(void);
};

这是我的界面文件:

  / *文件:example.i * / 
%模块示例

%{
#includeexample.h
%}

%包含example.h

我已经设法使用SWIG在Cygwin中使用以下命令包装我的c ++代码:

  $ swig -c ++ -python -o example_wrap.cpp示例。我的问题是,如何使用生成的代码从这一点创建一个DLL(example_wrap的.cpp)?有任何想法吗? 



我尝试使用Visual Studio C ++ 2010创建一个DLL,但是我收到了构建错误:

 链接:致命错误LNK1104:无法打开文件'python27_d.lib 

对于使用SWIG来说,相当新鲜,所以任何帮助将不胜感激。



谢谢!

解决方案

如果您查看Python安装的libs目录,我怀疑你会发现一个python27.lib而不是一个python27_d.lib。我相信_d.lib是Python库的调试版本,您的Python安装不包括它。在其他地方我已经看到它建议,最简单的方法是下载Python源码,并自己构建版本和调试版本,但我从未尝试过。或者,您可以修改构建以使用Python .lib的发行版本。你应该可以调试自己的代码,而不是Python代码。


I am in the process of learning how to use SWIG on Windows.

The following is my c++ code:

 /* File : example.cxx */

 #include "example.h"
 #define M_PI 3.14159265358979323846

/* Move the shape to a new location */
void Shape::move(double dx, double dy) {
    x += dx;
    y += dy;
 }

int Shape::nshapes = 0;

double Circle::area(void) {
   return M_PI*radius*radius;
}

double Circle::perimeter(void) {
  return 2*M_PI*radius;
}

double Square::area(void) {
  return width*width;
}

 double Square::perimeter(void) {
   return 4*width;
 }

This is my header file:

/* File : example.h */

   class Shape {
   public:
     Shape() {
     nshapes++;
   }
  virtual ~Shape() {
   nshapes--;
 };
  double  x, y;   
  void    move(double dx, double dy);
  virtual double area(void) = 0;
  virtual double perimeter(void) = 0;
  static  int nshapes;
  };

 class Circle : public Shape {
 private:
   double radius;
 public:
   Circle(double r) : radius(r) { };
   virtual double area(void);
   virtual double perimeter(void);
 };

 class Square : public Shape {
 private:
   double width;
 public:
   Square(double w) : width(w) { };
   virtual double area(void);
   virtual double perimeter(void);
 };

This is my interface file:

 /* File : example.i */
 %module example

 %{
 #include "example.h"
 %}

 %include "example.h"

I have managed to wrap my c++ code with the following command in Cygwin using SWIG:

  $swig -c++ -python -o example_wrap.cpp example.i 

My question is, how do I create a DLL from this point forward using the generated code (example_wrap.cpp)? Any ideas?

I tried creating a DLL with Visual Studio C++ 2010 but I get the build error:

LINK : fatal error LNK1104: cannot open file 'python27_d.lib

I'm fairly new to using SWIG so any help would be greatly appreciated.

Thanks!

解决方案

If you look in the libs directory of your Python installation I suspect you will find a python27.lib and not a python27_d.lib. I believe that the _d.lib is the debug version of the Python library and your Python installation didn't include it. Elsewhere I've seen it suggested that the simplest way around this is to download the Python sources and build the release and debug versions yourself but I've never tried this. Alternatively change you build to use the release version of the Python .lib. You should be able to debug your own code but not the Python code then.

这篇关于用SWIG从包装的cpp文件创建一个DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆