简体   繁体   English

如何使用SWIG生成跨平台接口?

[英]How to generate a cross platform interface with SWIG?

I'm wrapping a library with SWIG (Python as target). 我用SWIG(Python作为目标)包装一个库。 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. 库函数包含数据类型为“uint32_t”,“uint8_t”等的参数。我想尽可能创建跨平台的接口,所以我想在interface.i文件中使用原始函数签名。 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. 我试图解决的问题是SWIG不会将参数识别为整数,除非uint32_t数据类型上有typedef 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: 删除该typedef行将导致该函数无法从目标Python绑定中调用:

>>> 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. 以前的typedef在我的本地机器上是可以的,但在另一个编译器/平台上可能有所不同。 Using the directive %include "stdint.h" will raise an error on SWIG: 使用指令%include "stdint.h"将在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. 由于SWIG不是一个功能齐全的编译器,因此无法完全评估该标头上的所有#ifdef

How can I correctly feed SWIG with the datatypes the compiler is choosing on the stdint.h header? 如何使用编译器在stdint.h头文件中选择的数据类型正确地提供SWIG? Does in fact make sense to strictly enforce the correct datatypes, or just typedef ing all the intX_t to long is OK? 事实上确实是有意义的严格执行正确的数据类型,或者只是typedef荷兰国际集团所有intX_tlong是OK?

If you want to use these types in your SWIG interface file you can do something like: 如果要在SWIG界面文件中使用这些类型,可以执行以下操作:

%module test
%include "stdint.i"

uint32_t my_function();

Which is an existing SWIG interface has the correct typedef s for your system. 哪个现有SWIG接口具有适合您系统的typedef

You should force typedef because uint32_t is not cross platform and not cross compiler. 你应该强制使用typedef,因为uint32_t不是跨平台而不是交叉编译器。 uint32_t is C99 standard but many compilers decided not to implement completely this standard. uint32_t是C99标准,但许多编译器决定不完全实现这个标准。 You can have an include to redefine in a cross compiler way your project types: 您可以使用include以交叉编译器的方式重新定义项目类型:

http://www.azillionmonkeys.com/qed/pstdint.h http://www.azillionmonkeys.com/qed/pstdint.h

It is worth to read the introduction of the header I linked above. 值得阅读上面链接的标题的介绍。 You could use this include instead of stdint.h. 您可以使用此include而不是stdint.h。

You can also read this question: 你也可以阅读这个问题:

Cross-platform primitive data types in C++ C ++中的跨平台原始数据类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM