Sign in to follow this  
Prodigal Squirrel

Extending python with SWIG

Recommended Posts

I've been attempting to use SWIG with VC++ 6 in windows X to interface with python. I created a simple c++ source file and an interface file, and sucessfully ran swig on the interface file. I then created a simple DLL project in VC++ 6, included the source and wrapper files and set up neccessary directories and options. Now, I get 2 errors and a warning when I compile it. Here they are:
Linking...
pytonExt_wrap.obj : error LNK2005: "int __cdecl cppFunct(int)" (?cppFunct@@YAHH@Z) already defined in pythonExt.obj
pytonExt_wrap.obj : warning LNK4006: "int __cdecl cppFunct(int)" (?cppFunct@@YAHH@Z) already defined in pythonExt.obj; second definition ignored
   Creating library Debug/_pythonExt.lib and object Debug/_pythonExt.exp
Debug/_pythonExt.dll : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.

_pythonExt.dll - 2 error(s), 1 warning(s)
I do not understand at all why this is happening, any ideas?

Share this post


Link to post
Share on other sites
I've been using SWIG to provide a C# interface.

You say you added both the source and the wrapper files. You should compile your source into a lib and compile the wrapper seperately into a lib. The wrapper will eventually call your original code, but it's just a layer on top and must be compiled seperately.

hth,
CipherCraft

Share this post


Link to post
Share on other sites
Have you taken a look at Pyrex yet?
I'm sharing this in case you're doing some exploring to find alternative ways to create Python extensions. I've used SWIG too, and Pyrex seems a lot nicer to work with.

Share this post


Link to post
Share on other sites
I don't think this is directly caused by swig.

Did you write "cppFunct" in an header file?

Share this post


Link to post
Share on other sites
Well, that's your problem.

The function will be compiled twice.

Once in the compilation of pyext.cpp

and another time in the compilation of pyext_wrap.cxx (because the content of the pyext.cpp is included in it).

You should have an header file for the functions pyext.cpp and include it in your interface file instead.

Share this post


Link to post
Share on other sites
Quote:
Original post by Gorg
Well, that's your problem.

The function will be compiled twice.

Once in the compilation of pyext.cpp

and another time in the compilation of pyext_wrap.cxx (because the content of the pyext.cpp is included in it).

You should have an header file for the functions pyext.cpp and include it in your interface file instead.


So, do I just change the etension of my source file to a .h file? Then go about the process as normal? And is this how my interface file should be set up?

%module pythonExt
%{
#include "pythonext.hpp"
%}

extern int cppFunct(int x);

Share this post


Link to post
Share on other sites
I meant something like this :

PythonExt.hpp

#ifndef PYTHONEXT_HPP
#define PYTHONEXT_HPP

int cppFunct(int x);

#endif //PYTHONEXT_HPP


PythonExt.cpp

#include "PythonExt.hpp"

int cppFunct(int x)
{
//whatever it does
}


PythonExt.i


%module pythonExt;

%{
#include "PythonExt.hpp"
%}

int cppFunct(int x);


run swig on the interface file and compile both PythonExt.cpp and pythonExt_wrap.cxx.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this