Creating a DLL from a wrapped cpp file with SWIG

2019-02-10 02:29发布

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!

标签: python dll swig
8条回答
混吃等死
2楼-- · 2019-02-10 03:21

You can try adding "python27_d.lib" (without quotes) to ignored libs:

Configuration Properties -> Linker -> Input -> Ignore Specific Library

查看更多
一纸荒年 Trace。
3楼-- · 2019-02-10 03:23

I resolved the missing python27_d.lib by doing the following:

  • Copy python27.lib to python27_d.lib
  • In pyconfig.h comment out define Py_DEBUG
查看更多
登录 后发表回答