Extract the fields of a C struct

喜夏-厌秋 提交于 2019-12-29 17:53:07

问题


I often have to write code in other languages that interact with C structs. Most typically this involves writing Python code with the struct or ctypes modules.

So I'll have a .h file full of struct definitions, and I have to manually read through them and duplicate those definitions in my Python code. This is time consuming and error-prone, and it's difficult to keep the two definitions in sync when they change frequently.

Is there some tool or library in any language (doesn't have to be C or Python) which can take a .h file and produce a structured list of its structs and their fields? I'd love to be able to write a script to generate my automatically generate my struct definitions in Python, and I don't want to have to process arbitrary C code to do it. Regular expressions would work great about 90% of the time and then cause endless headaches for the remaining 10%.


回答1:


If you compile your C code with debugging (-g), pahole (git) can give you the exact structure layouts being used.

$ pahole /bin/dd
…
struct option {
        const char  *              name;                 /*     0     8 */
        int                        has_arg;              /*     8     4 */

        /* XXX 4 bytes hole, try to pack */

        int *                      flag;                 /*    16     8 */
        int                        val;                  /*    24     4 */

        /* size: 32, cachelines: 1, members: 4 */
        /* sum members: 24, holes: 1, sum holes: 4 */
        /* padding: 4 */
        /* last cacheline: 32 bytes */
};
…

This should be quite a lot nicer to parse than straight C.




回答2:


Regular expressions would work great about 90% of the time and then cause endless headaches for the remaining 10%.

The headaches happen in the cases where the C code contains syntax that you didn't think of when writing your regular expressions. Then you go back and realise that C can't really be parsed by regular expressions, and life becomes not fun.

Try turning it around: define your own simple format, which allows less tricks than C does, and generate both the C header file and the Python interface code from your file:

define socketopts
    int16 port
    int32 ipv4address
    int32 flags

Then you can easily write some Python to convert this to:

typedef struct {
    short port;
    int ipv4address;
    int flags;
} socketopts;

and also to emit a Python class which uses struct to pack/unpack three values (possibly two of them big-endian and the other native-endian, up to you).




回答3:


Have a look at Swig or SIP that would generate interface code for you or use ctypes.




回答4:


Have you looked at Swig?




回答5:


I have quite successfully used GCCXML on fairly large projects. You get an XML representation of the C code (including structures) which you can post-process with some simple Python.




回答6:


ctypes-codegen or ctypeslib (same thing, I think) will generate ctypes Structure definitions (also other things, I believe, but I only tried structs) by parsing header files using GCCXML. It's no longer supported, but will likely work in some cases.




回答7:


One my friend for this tasks done C-parser which he use with cog.



来源:https://stackoverflow.com/questions/2050318/extract-the-fields-of-a-c-struct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!