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?
The most common format of a SWIG interface is as follows: %module mymodule %{ #include "myheader. h" %} // Now list ANSI C/C++ declarations int foo; int bar(int x); ... The module name is supplied using the special %module directive.
SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages. SWIG is used with different types of target languages including common scripting languages such as Javascript, Perl, PHP, Python, Tcl and Ruby.
We use the SWIG %apply directive to apply the typemap for one-dimensional input arrays of type double to the actual prototype used by rms . Using numpy.
The SWIG %module directive specifies the name of the Python module. If you specify `%module example', then everything is wrapped into a Python 'example' module. Underneath the covers, this module consists of a Python source file example.py and a low-level extension module _example.so.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With