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.
add MS_NO_COREDLL definition at Configuration Properties->C/C++->Preprocessor->Preprocessor Definitions; or add #define MS_NO_COREDLL line before including python.h.
Building the project in Release mode removes the python27_d.lib dependency too; at least it did for my own project.
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:
SWIG (at least on v3.0) generates the python.h inclusion in the wrapper as follows:
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 thepyconfig.h
file issue mentioned in Ken's answer.I found out that addind the Python symbols do the Project solves it. Do it like this
I also copied the python27.lib to a file named python27_d.lib