Skip to main content
GameDev.net gamedev.net
🔒 Locked

Extending python with SWIG

Started by Prodigal Squirrel Mar 28, 2006 at 1:47 AM 10 replies 2.9k views
Original Post
Prodigal Squirrel
Prodigal Squirrel
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?
-------------------------- Alex--------------------------
CipherCraft
CipherCraft
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
Prodigal Squirrel
Prodigal Squirrel
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.
-------------------------- Alex--------------------------
Redleaf
Redleaf
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.
Gorg
Gorg
I don't think this is directly caused by swig.

Did you write "cppFunct" in an header file?
Prodigal Squirrel
Prodigal Squirrel
I have cppFunct(int x) defined in a .cpp file, which is used by my .i file.
-------------------------- Alex--------------------------
Gorg
Gorg
Do you mean you do this?

interface.i
%{  #include "myfile.cpp"%}
Prodigal Squirrel
Prodigal Squirrel
Yes, as a matter of fact, this is what my .i file looks like.

%module pyext

%{
#include "pyext.cpp"
%}

%include "pyext.cpp"
-------------------------- Alex--------------------------
Gorg
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.
Prodigal Squirrel
Prodigal Squirrel
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);
-------------------------- Alex--------------------------
Gorg
Gorg
I meant something like this :

PythonExt.hpp
#ifndef PYTHONEXT_HPP#define PYTHONEXT_HPPint 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.
Prodigal Squirrel
Prodigal Squirrel
OMFG, thank you! I've been working on this for so long.

Thank you for your persistance and help.
-------------------------- Alex--------------------------

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.