简体   繁体   English

使用SWIG包装几个C源(.c文件)以在Python中使用它们的函数

[英]Wrap several C-Sources (.c files) to use their functions in Python using SWIG

There are a lot of Tutorials about how to wrap single functions of one .c file. 有很多关于如何包装一个.c文件的单个函数的教程。 But I want to wrap all functions of several .c files. 但我想包装几个.c文件的所有功能。 This is why I included them all in the interface file (interface.i) 这就是我将它们全部包含在接口文件(interface.i)中的原因

/*interface.i*/
%module interface
%include source1.c
%include source2.c
%include source3.c

I am now wondering if I have to include all header files which appear in the .c files. 我现在想知道是否必须包含出现在.c文件中的所有头文件。 And also if I have to consider the header files which are addressed in the header files. 如果我必须考虑头文件中寻址的头文件。

Usually your C program should have an interface it exposes. 通常你的C程序应该有一个它暴露的接口。 For example the source1/2/3.c together with their internal headers source1/2/3.h compile to a foo.dll. 例如,source1 / 2 / 3.c及其内部头文件source1 / 2 / 3.h编译为foo.dll。 Then the interface foo_function.h exposes functions that can be called from outside. 然后接口foo_function.h公开了可以从外部调用的函数。

To wrap the external functions you need only the foo_functions.h in your interface.i. 要包装外部函数,只需要在interface.i中使用foo_functions.h。 You don't need the source1/2/3.h. 您不需要source1 / 2 / 3.h.
The C code may have another foo_constants.h or so, belonging to the interface, with the structs you need and constant definitions that are quite handy. C代码可能有另一个foo_constants.h左右,属于接口,具有您需要的结构和非常方便的常量定义。 You should add that too. 你也应该添加它。

Additionally you may need some includes from SWIG itself. 此外,您可能需要SWIG本身的一些包含。 To generate for example pointer and array handling. 生成例如指针和数组处理。 Something like: 就像是:
%include cpointer.i %包括cpointer.i
%pointer_functions(int, intp) %pointer_functions(int,intp)
%include carrays.i %包括carrays.i
%array_functions(unsigned int, uintarray) %array_functions(unsigned int,uintarray)

Good luck! 祝好运!

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

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