• 13
• 18
• 19
• 27
• 10

# Extending python with SWIG

This topic is 4373 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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

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

##### 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 on other sites
Does that mean I create a dll file for both the wrapper and source? Is a DLL the same as a libraray?

Sorry, I'm fairly new to the c programming languages.

##### 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 on other sites
I don't think this is directly caused by swig.

Did you write "cppFunct" in an header file?

##### Share on other sites
I have cppFunct(int x) defined in a .cpp file, which is used by my .i file.

##### Share on other sites
Do you mean you do this?

interface.i
%{  #include "myfile.cpp"%}

##### Share on other sites
Yes, as a matter of fact, this is what my .i file looks like.

%module pyext

%{
#include "pyext.cpp"
%}

%include "pyext.cpp"

##### Share on other sites

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 on other sites
Quote:
 Original post by GorgWell, that's your problem.The function will be compiled twice.Once in the compilation of pyext.cppand 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);