Pero es dificil que sea una buena idea, a no ser que lo que se quiera usar sea algo muy sencillito.
Según la documentación recomiendan una serie de pasos:
- "Identify the functions that you want to wrap. It's probably not necessary to access every single function in a C program--thus, a little forethought can dramatically simplify the resulting scripting language interface. C header files are particularly good source for finding things to wrap.
- Create a new interface file to describe the scripting language interface to your program.
- Copy the appropriate declarations into the interface file or use SWIG's %include directive to process an entire C source/header file.
- Make sure everything in the interface file uses ANSI C/C++syntax.
- Make sure all necessary `typedef' declarations and type-information is available in the interface file.
- If your program has a main() function, you may need to rename it (read on).
- Run SWIG and compile."
Resumiendo.... que mejor solo hagas bindings para lo que realmente necesitas.
A continuación pongo otra parrafada interesante, sobre porque usar interface files.
"Although SWIG can parse many header files, it is more common to write a special .i file defining the interface to a package.
There are several reasons why you might want to do this:
- It is rarely necessary to access every single function in a large package. Many C functions might have little or no use in a scripted environment. Therefore, why wrap them?
- Separate interface files provide an opportunity to provide more precise rules about how an interface is to be constructed.
- Interface files can provide more structure and organization.
- SWIG can't parse certain definitions that appear in header files. Having a separate file allows you to eliminate or work around these problems.
- Interface files provide a more precise definition of what the interface is. Users wanting to extend the system can go to the interface file and immediately see what is available without having to dig it out of header files."
Un ejemplo de dos posibilidades de definicion. Una primera teniendo el fichero de interfaz:
/* File : interface.i */
%module mymodule
%{
#include "header.h"
%}
extern int foo(double);
extern double bar(int, int);
extern void dump(FILE *f);
Y la segunda (que en este caso estaría justificada por ser el fichero muy sencillo):
/* File : interface.i */
%module mymodule
%include header.h
Por ejemplo si usamos la ultima aproximación con libcroco, debemos usar el flag --includeall e indicar con -I las rutas de todos los ficheros necesarios (incluyendo los del sistema), generandose mucho codigo, cuando lo que necesitaremos será simplemente una función y acceder a unas estructuras de datos.
Es mas.... posiblemente al final la aproximacion mas facil será crear unas funciones de utilidad en C, y ya hacer los wrappers a dichas funciones de utilidad con swig.
Nota: un flag interesante es -v.
No hay comentarios:
Publicar un comentario