简体   繁体   中英

Getting my python c++ extension to compile using Swig and Distutils on Windows

I'm trying to create a python c++ extension on windows. My problem is that I can't seem to generate the .pyd file (link error) even after using swig and distutils. Below is step by step what I did:

In Microsoft Visual Studio 2010 I created a class 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);
      };  

Then I created an .i file HelloWorld.i

%module HelloWorld

%{
#include "Hello.h"
%}

%include "Hello.h"

Then I swigged

swig -c++ -python -o Hello_wrap.cpp HelloWorld.i

This seemed to have worked successfully, generating the files I expected it would. Next I created 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])

With that written and after switching into the directory where all the files are stored together, I entered into the command line

python setup.py build

Unfortuantely, this is the error I get

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

What do I do about this link error? I'm assuming swig and distutils are working correctly, so I wouldn't have to change the code. Help please! Thanks

try the following

module1=Extension('_HelloWorld', sources=['Hello.cpp'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM