Swig, Python, and C++

Mich Peffe locopeffe1 at yahoo.com
Thu Mar 2 23:56:08 EST 2006


Greetings All,

I'm using SWIG for the first time and am running into some problems.  I've tried researching at swig.org and python.org, but unfortunately, most of the examples use programs I don't have, such as Solaris, Unix, and Irix.  I wish to make my C++ classes and functions accessible from Python, and create the glue code necessary to interface with my compiler.  I'm using Python 2.4, MS Visual Studio 2005, and Windows XP command prompt.

C++ Code:
/* FuncTest.h */

#include <time.h>

int fact(int n)
{
    if(n <=1) return 1;
    else return n*fact(n-1);
}

char *get_time()
{
    time_t ltime;
    time(&ltime);
    return ctime(&ltime);
}

Interface File:
/* example.i */
%module example
%{
#include "FuncTest.h"
%}

%include "FuncTest.h"

Command Prompt Input:
c:\Utilities\Swig\swig -python -c++ example.i

example.py and example_wrap.cxx are then created.  

This is where I hit the wall.  example_wrap.cxx can't be included in my project because it creates 2 linker errors:
 
error LNK2005: _SWIG_init already defined in Main.obj    PENT_wrap.obj
fatal error LNK1169: one or more multiply defined symbols found    C:\My Work\PEntityTest\Release\PEntityTest.exe    1

These errors are because of this chunk of code created by SWIG:
#ifdef __cplusplus
extern "C"
#endif
SWIGEXPORT void SWIG_init(void) {
  PyObject *m, *d;
  
  /* Fix SwigMethods to carry the callback ptrs when needed */
  SWIG_Python_FixMethods(SwigMethods, swig_const_table, swig_types, swig_type_initial);
  
  m = Py_InitModule((char *) SWIG_name, SwigMethods);
  d = PyModule_GetDict(m);
  
  SWIG_InitializeModule(0);
  SWIG_InstallConstants(d,swig_const_table);
  
  
}

I can't import the example.py module because the generated script tries to import _example.

I assume this file is created in another step that I haven't figured out how to do yet. The SWIG tutorial instructs the following:

unix % swig -python example.i
 unix % gcc -c example.c example_wrap.c \
        -I/usr/local/include/python2.1
 unix % ld -shared example.o example_wrap.o -o _example.so 
I believe this is what I need to do, but using XP command prompt.

Any feedback on any one little chunk of this huge post would be helpful.

Much thanks.

-Mich





			
---------------------------------
 Yahoo! Mail
 Use Photomail to share photos without annoying attachments.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060302/2af8b45f/attachment.html>


More information about the Python-list mailing list