Sign in to follow this  
ove123

Help getting started with SWIG

Recommended Posts

Hi i'm trying to use SWIG for the first time. I want to use some C functions from Python. I have a example.i file that looks like this: %module example %{ %} int testing(int a, int b); And my C code in example.c: int testing(int a, int b) { return a + b; } Now i used swig -python example.i This generated a example_wrap.c and a example.py. But what else do I need to get it to work? If I run the python file i get some error "No module named _example" So I guess I need to compile it somehow? I can only find unix examples of how to do it?: "unix % gcc -fpic -c example.c example_wrap.c -I/usr/local/include unix % gcc -shared example.o example_wrap.o -o example.so" But how do I fix this in Windows? I use Visual Studio 2005. Thanks

Share this post


Link to post
Share on other sites
You could get a windows port if the gcc compiler.
I use DJGPP but there's also MinGW. or you could figure out how to do this with the microsoft compiler.
I hope that helps. :)

Share this post


Link to post
Share on other sites
Thanks for the reply, I guess I'll try one of those. But if anyone knows how to do it with Visual studio 2005 I would be really thankfull.

Share this post


Link to post
Share on other sites
Here's a bat file I used to do some SWIG builds with Visual C++:


set PYLIB=E:\Python23\libs
set PYINCLUDE=E:\Python23\Include
swig -python TrapezCpp.i
cl /LD /I %PYINCLUDE% TrapezCpp.c TrapezCpp_wrap.c /Fe_TrapezC.dll /link /LIBPATH:%PYLIB%



I'll include some of the other files for good measure. TrapezCpp.i:

/* Arild Fines */
/* $Id$ */

%module TrapezC
%{
#include "TrapezC.h"
%}

%include "TrapezC.h"

%constant FUNC F1 = f_1;
%constant FUNC F2 = f_2;



TrapezC.h:

#if !defined(TRAPEZC_H)
#define TRAPEZC_H

typedef double(*FUNC)(double);

double Trapez( double a, double b, FUNC func, int n );

double f_1( double );
double f_2( double );
#endif


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