【问题标题】:Creating a DLL from a wrapped cpp file with SWIG使用 SWIG 从包装的 cpp 文件创建 DLL
【发布时间】:2012-07-03 13:21:47
【问题描述】:

我正在学习如何在 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 还很陌生,因此我们将不胜感激。

谢谢!

【问题讨论】:

    标签: python dll swig


    【解决方案1】:

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

    #define MS_NO_COREDLL
    #include <Python.h>
    

    【讨论】:

    • 非常感谢,对我编译python2.5版本源代码很有帮助。但是你能告诉我为什么要添加这个吗?
    • 在此之后我得到错误 LNK2001: unresolved external symbol _Py_RefTotal
    【解决方案2】:

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

    【讨论】:

    • 感谢 Jackson 的及时答复! :) 我检查了 Python 文件,正如你所说,似乎有一个 python27.lib,但没有一个 python27_d.lib。所以我想下一个问题是……有谁知道让 Visual Studio c++ 2010 接受 Python lib 发行版的方法???
    • 如果您打开项目的属性并转到 Linker->Input,您应该会在 Additional Dependencies 中看到对 Python27_d.lib 的引用。将其编辑为 Python27.lib 并尝试重新编译。
    • 再次感谢 Jackson 抽出宝贵时间!我回顾了 Visual Studio 2010 Linker>Input 但没有找到 Python27_d.lib。最后,我决定坚持使用cygwin。还是谢谢你!
    【解决方案3】:

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

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

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

    【讨论】:

      【解决方案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 文件问题。

      【讨论】:

        【解决方案5】:

        在发布模式下构建项目也会删除 python27_d.lib 依赖项;至少对我自己的项目是这样。

        【讨论】:

          【解决方案6】:

          我发现添加 Python 符号可以解决这个问题。像this那样做

          我还将 python27.lib 复制到名为 python27_d.lib 的文件中

          【讨论】:

            【解决方案7】:

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

            配置属性 -> 链接器 -> 输入 -> 忽略特定库

            【讨论】:

              【解决方案8】:

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

              • 将 python27.lib 复制到 python27_d.lib
              • 在 pyconfig.h 中注释掉 define Py_DEBUG

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2017-05-02
                • 2012-02-17
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2023-04-05
                • 1970-01-01
                相关资源
                最近更新 更多