2
我试图在Windows上创建一个python C++扩展。我的问题是,即使使用swig和distutils后,我似乎也无法生成.pyd文件(链接错误)。下面是一步一步我所做的:在Windows上使用Swig和Distutils获取我的python C++扩展
在Microsoft Visual Studio 2010中,我创建一个类 HELLO.CPP:
#include "StdAfx.h"
#include "Hello.h"
#include <iostream>
using namespace std;
Hello::Hello(void)
{
}
void Hello::greeting(void){
cout<<"Hello World!!"<<endl;
}
Hello::~Hello(void)
{
}
Hello.h:
#pragma once
class Hello
{
public:
Hello(void);
~Hello(void);
void greeting(void);
};
然后,我创建了一个。我的文件HelloWorld.i
%module HelloWorld
%{
#include "Hello.h"
%}
%include "Hello.h"
然后我swigged
swig -c++ -python -o Hello_wrap.cpp HelloWorld.i
这似乎工作成功,生成我期望它会的文件。接下来我创建了setup.py。
from distutils.core import setup, Extension
module1=Extension('HelloWorld', sources=['Hello.cpp'])
setup(name='Hello_Package', version='1.0', description='This is a demo', \
ext_modules=[module1])
随着该书面和切换到其中的所有文件一起保存的目录后,我进入命令行
python setup.py build
Unfortuantely,这是错误我得到
running build
running build_ext
building 'HelloWorld' extension
creating build
creating build\temp.win32-2.7
creating build\temp.win32-2.7\Release
c:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\cl.exe /c /nologo /Ox /MD /W
3 /GS- /DNDEBUG -IC:\Python27\include -IC:\Python27\PC /TpHello.cpp /Fobuild\tem
p.win32-2.7\Release\Hello.obj
Hello.cpp
c:\Program Files\Microsoft Visual Studio 9.0\VC\INCLUDE\xlocale(342) : warning C
4530: C++ exception handler used, but unwind semantics are not enabled. Specify
/EHsc
creating build\lib.win32-2.7
c:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\link.exe /DLL /nologo /INCRE
MENTAL:NO /LIBPATH:C:\Python27\libs /LIBPATH:C:\Python27\PCbuild /EXPORT:initHel
loWorld build\temp.win32-2.7\Release\Hello.obj /OUT:build\lib.win32-2.7\HelloWor
ld.pyd /IMPLIB:build\temp.win32-2.7\Release\HelloWorld.lib /MANIFESTFILE:build\t
emp.win32-2.7\Release\HelloWorld.pyd.manifest
LINK : error LNK2001: unresolved external symbol initHelloWorld
build\temp.win32-2.7\Release\HelloWorld.lib : fatal error LNK1120: 1 unresolved
externals
error: command '"c:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\link.exe"'
failed with exit status 1120
我该怎么做这个链接错误?我假设swig和distutils工作正常,所以我不必更改代码。请帮助!感谢