我正在学习如何在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;
}
Run Code Online (Sandbox Code Playgroud)
这是我的头文件:
/* 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);
};
Run Code Online (Sandbox Code Playgroud)
这是我的界面文件:
/* File : example.i */
%module example
%{
#include "example.h"
%}
%include "example.h"
Run Code Online (Sandbox Code Playgroud)
我已经设法使用SWIG在Cygwin中使用以下命令包装我的c ++代码:
$swig -c++ -python -o example_wrap.cpp example.i
Run Code Online (Sandbox Code Playgroud)
我的问题是,如何使用生成的代码(example_wrap.cpp)从这一点开始创建DLL?有任何想法吗?
我尝试使用Visual Studio C++ 2010创建DLL,但是我收到了构建错误:
LINK : fatal error LNK1104: cannot open file 'python27_d.lib
Run Code Online (Sandbox Code Playgroud)
我是使用SWIG的新手,所以任何帮助都将非常感激.
谢谢!
小智 12
在Configuration Properties-> C/C++ - > Preprocessor-> Preprocessor Definitions中添加MS_NO_COREDLL定义; 或者在包含python.h之前添加#define MS_NO_COREDLL行.
#define MS_NO_COREDLL
#include <Python.h>
Run Code Online (Sandbox Code Playgroud)
如果查看Python安装的libs目录,我怀疑你会发现python27.lib而不是python27_d.lib.我相信_d.lib是Python库的调试版本,并且您的Python安装不包含它.在其他地方,我已经看到它建议最简单的方法是下载Python源代码并自己构建发布和调试版本,但我从未尝试过这个.或者,更改构建以使用Python .lib的发行版.您应该能够调试自己的代码,但不能调试Python代码.
小智 5
问题似乎是,由于未知原因,文件pyconfig.h FORCES使用了一个专门命名的.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 */
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
因此,在Windows平台上编译包装程序的调试版本时,我们只需要定义SWIG_PYTHON_INTERPRETER_NO_DEBUG标志以避免pyconfig.hKen的答案中提到的文件问题。
| 归档时间: |
|
| 查看次数: |
12207 次 |
| 最近记录: |