[SciPy-dev] Calling extrenal functions with weave

eric jones eric at enthought.com
Mon Mar 10 11:49:08 CST 2003


Hey Christophe,

Yes.  Here is an example:

$ gcc -c foo.c
$ python
>>> import weave
>>> var = 1
>>> weave.inline("return_val = bar(var);",['var'],
                 headers=['"foo.h"'],
                 extra_objects=['foo.o'],
                 include_dirs=['.'])
2

The code for foo.c and foo.h are below.  Note that the .h file has
#ifdefs to check for a C++ compiler (which weave uses).  This keeps the
C++ compiler from mangling the name of the bar() function so that it
finds the correct function in foo.o (built with a non-mangling C
compiler).  If foo.c was compiled using "g++ -c foo.c", the foo.h file
wouldn't need these #ifdefs.

Hope that helps,
eric

/*------------------------------------*/
/* foo.c */
#include "foo.h"

int bar(int a)
{
    return a + 1;
}

/*------------------------------------*/
/* foo.h */

#ifndef FOO_H
#define FOO_H

#ifdef __cplusplus
extern "C" {
#endif

int bar(int a);

#ifdef __cplusplus
}
#endif

#endif


----------------------------------------------
eric jones                    515 Congress Ave
www.enthought.com             Suite 1614
512 536-1057                  Austin, Tx 78701 


> -----Original Message-----
> From: scipy-dev-admin at scipy.net [mailto:scipy-dev-admin at scipy.net] On
> Behalf Of christophe grimault
> Sent: Monday, March 10, 2003 3:35 AM
> To: scipy-dev at scipy.org
> Subject: [SciPy-dev] Calling extrenal functions with weave
> 
> Hi,
> 
> I was just wandering if it is possible, inside a code """ ..... """,
to
> call a function foo(u), compiled in a foo.o file ?
> 
> Thanks in advance.
> 
> Christophe Grimault
> 
> 
> _______________________________________________
> Scipy-dev mailing list
> Scipy-dev at scipy.net
> http://www.scipy.net/mailman/listinfo/scipy-dev





More information about the Scipy-dev mailing list