使用SWIG从包装的cpp文件创建DLL

时间:2012-07-03 13:21:48

标签: python dll swig

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

以下是我的c ++代码:

 /* 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;
 }

这是我的头文件:

/* 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);
 };

这是我的界面文件:

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

 %{
 #include "example.h"
 %}

 %include "example.h"

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

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

我的问题是,如何使用生成的代码(example_wrap.cpp)从这一点开始创建DLL?有任何想法吗?

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

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

我是使用SWIG的新手,所以非常感谢任何帮助。

谢谢!

8 个答案:

答案 0 :(得分:12)

在配置属性中添加MS_NO_COREDLL定义 - > C / C ++ - >预处理器 - >预处理器定义; 或者在包含python.h之前添加#define MS_NO_COREDLL行。

#define MS_NO_COREDLL
#include <Python.h>

答案 1 :(得分:5)

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

答案 2 :(得分:5)

问题似乎是,由于未知原因,文件pyconfig.h FORCES使用了一个专门命名的.lib文件。哎哟!坦率地说,这看起来像是一个错误 - 让程序员指定使用什么.lib文件!不要强迫它!

在下面的代码中,您可以简单地#ifdef 0整个事物,或者将“python27_d”重命名为 “蟒蛇”。

无论如何,这是来自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 */

答案 3 :(得分:4)

SWIG(至少在v3.0上)在包装器中生成python.h包含如下:

#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

因此,在Windows平台上编译包装器的调试版本时,我们只需要定义SWIG_PYTHON_INTERPRETER_NO_DEBUG标志,以避免Ken的回答中提到的pyconfig.h文件问题。

答案 4 :(得分:2)

在Release模式下构建项目也会删除python27_d.lib依赖项;至少它是为我自己的项目做的。

答案 5 :(得分:0)

我发现addind Python符号是Project解决它的。就像this

一样

我还将python27.lib复制到名为python27_d.lib

的文件中

答案 6 :(得分:0)

您可以尝试将“python27_d.lib”(不含引号)添加到忽略的库中:

配置属性 - &gt;链接器 - &gt;输入 - &gt;忽略特定图书馆

答案 7 :(得分:0)

我通过执行以下操作解决了缺少的python27_d.lib:

  • 将python27.lib复制到python27_d.lib
  • 在pyconfig.h中注释掉info_btn.imageView.contentMode = .scaleAspectFit