Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a DLL from a wrapped cpp file with SWIG

Tags:

python

dll

swig

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!

like image 389
user1449530 Avatar asked Jul 03 '12 13:07

user1449530


4 Answers

add MS_NO_COREDLL definition at Configuration Properties->C/C++->Preprocessor->Preprocessor Definitions; or add #define MS_NO_COREDLL line before including python.h.

#define MS_NO_COREDLL
#include <Python.h>
like image 57
guangboo Avatar answered Oct 31 '22 17:10

guangboo


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.

like image 37
Jackson Avatar answered Oct 31 '22 17:10

Jackson


The problem seems to be that, for unknown reasons, the file pyconfig.h FORCES the use of a specifically named .lib file. OUCH! Frankly, this looks like a bug to me - let the programmer specify what .lib file to use! Don't force it!

In the code below, you could simply #ifdef 0 the entire thing, or rename "python27_d" to "python".

Anyway, here is the offensive code from pyconfig.h:

/* For an MSVC DLL, we can nominate the .lib files used by extensions
*/
#ifdef MS_COREDLL
#   ifndef Py_BUILD_CORE /* not building the core - must be an ext */
#       if defined(_MSC_VER)            /* So MSVC users need not specify the .lib file in          their Makefile (other compilers are generally           taken care of by distutils.) */
#           ifdef _DEBUG
#               pragma comment(lib,"python27_d.lib")
#           else
#               pragma comment(lib,"python27.lib")
#           endif /* _DEBUG */
#       endif /* _MSC_VER */
#   endif /* Py_BUILD_CORE */
#endif /* MS_COREDLL */
like image 29
Ken Demarest Avatar answered Oct 31 '22 18:10

Ken Demarest


SWIG (at least on v3.0) generates the python.h inclusion in the wrapper as follows:

#if defined(_DEBUG) && defined(SWIG_PYTHON_INTERPRETER_NO_DEBUG)
/* Use debug wrappers with the Python release dll */
# undef _DEBUG
# include <Python.h>
# define _DEBUG
#else
# include <Python.h>
#endif

So when compiling a debug version of the wrapper on a Windows platform, we simply need to define the SWIG_PYTHON_INTERPRETER_NO_DEBUG flag to avoid the pyconfig.h file issue mentioned in Ken's answer.

like image 31
greydet Avatar answered Oct 31 '22 17:10

greydet