Basic question about SWIG

Joe Mason jcmason at student.math.uwaterloo.ca
Tue Nov 20 16:22:58 EST 2001


I'm trying to make a Python wrapper for a C library using SWIG.  The library
API is defined in a single header file (http://www.eblong.com/zarf/glk/glk.h)
After applying the patch at the end of this post, I'm trying to run swig
directly on the .h file ("swig -python glk.h")

The problem I'm running into is with typedefs.  The .h file does: 

  typedef unsigned long glui32;

  ...

  extern glui32 glk_gestalt(glui32 sel, glui32 val);
  extern glui32 glk_gestalt_ext(glui32 sel, glui32 val, glui32 *arr,
    glui32 arrlen);

It seems to me from reading the user's manual that SWIG should pick up this
typedef and know enough to treat glui32 as an int thereafter.  But when I
try to compile the glk_wrap.c file, I get:

glk_wrap.c:558: warning: parameter names (without types) in function declarationglk_wrap.c:559: parse error before `glk_gestalt'
glk_wrap.c:559: warning: parameter names (without types) in function declarationglk_wrap.c:559: warning: data definition has no type or storage class
glk_wrap.c:560: parse error before `glk_gestalt_ext'
glk_wrap.c:560: parse error before `*'

Followed by about a thousand more errors for the other functions using glui32.

Checking glk_wrap.c I find that the lines it's complaining about are:

extern glui32 glk_gestalt(glui32 ,glui32 );
extern glui32 glk_gestalt_ext(glui32 ,glui32 ,glui32 *,glui32 );

And glui32 isn't defined anywhere in the file.

Here's a minimal interface file showing the problem:

    %module test
    %{
      testint add_to_a(testint a)
      {
        return a+1;
      }
    %}

    typedef int testint;
    extern testint add_to_a(testint a);

So what's my bad assumption?  Why is SWIG ignoring these typedefs?

Also, I was under the impression that if glui32 weren't typedef'd to int, SWIG
would assume it was a complex structure and create wrappers to treat it as an
opaque structure, but that doesn't seem to be happening either.  Why not?

Joe

(Here's the patch if anyone wants to try this with glk.h itself.  I know
there'll be other problems with it, though, due to weirdnesses in the 
structure of the glk libraries.)

--- ../glk.h    Tue Nov 20 14:45:01 2001
+++ glk.h       Tue Nov 20 14:59:38 2001
@@ -1,3 +1,7 @@
+#ifdef SWIG
+%module glk
+#endif
+
 #ifndef GLK_H
 #define GLK_H

@@ -171,8 +175,10 @@
 extern void glk_main(void);

 extern void glk_exit(void);
-extern void glk_set_interrupt_handler(void (*func)(void));
 extern void glk_tick(void);
+
+typedef void (*glk_interrupt_func)(void);
+extern void glk_set_interrupt_handler(glk_interrupt_func func);

 extern glui32 glk_gestalt(glui32 sel, glui32 val);
 extern glui32 glk_gestalt_ext(glui32 sel, glui32 val, glui32 *arr,



More information about the Python-list mailing list