How to generate a cross platform interface with SWIG?
c, gcc, python, swig
Solution
If you want to use these types in your SWIG interface file you can do something like:
%module test
%include "stdint.i"
uint32_t my_function();
Which is an existing SWIG interface has the correct `typedef`s for your system.
Problem
I'm wrapping a library with SWIG (Python as target). The library functions contains parameters with the datatypes "uint32_t", "uint8_t", etc. I want to create the interface as cross-platform as possible, so I want to use the original function signatures in my `interface.i` file. For example: ``` uint32_t func(uint32_t a, uint32_t b); ``` The issue I'm trying to solve is that SWIG won't recognize the parameter as an integer unless there is a `typedef` on the `uint32_t` datatype. Right now I'm using this on the interface file: ``` typedef unsigned uint32_t; ``` Removing that `typedef` line will cause the function not to be callable from the target Python binding: ``` >>> mylib.func(2, 2) TypeError: in method 'func', argument 1 of type 'uint32_t' ``` The previous `typedef` is OK on my local machine, but might be different on another compiler/platform. Using the directive `%include "stdint.h"` will raise an error on SWIG: ``` /usr/include/stdint.h:44: Error: Syntax error in input(1). ``` Wich makes sense since SWIG is not a full-featured compiler, and can not fully evaluate all the `#ifdef` on that header. How can I correctly feed SWIG with the datatypes the compiler is choosing on the `stdint.h` header? Does in fact make sense to strictly enforce the correct datatypes, or just `typedef`ing all the `intX_t` to `long` is OK?