How can I reference #defines in a C file from python?

前端 未结 5 1741
星月不相逢
星月不相逢 2020-12-30 10:45

I have a C file that has a bunch of #defines for bits that I\'d like to reference from python. There\'s enough of them that I\'d rather not copy them into my python code, i

5条回答
  •  半阙折子戏
    2020-12-30 11:25

    I had almost exactly this same problem so wrote a Python script to parse the C file. It's intended to be renamed to match your c file (but with .py instead of .h) and imported as a Python module.

    Code: https://gist.github.com/camlee/3bf869a5bf39ac5954fdaabbe6a3f437

    Example:

    configuration.h

    #define VERBOSE 3
    #define DEBUG 1
    #ifdef DEBUG
        #define DEBUG_FILE "debug.log"
    #else
        #define NOT_DEBUGGING 1
    #endif
    

    Using from Python:

    >>> import configuration
    >>> print("The verbosity level is %s" % configuration.VERBOSE)
    The verbosity level is 3
    >>> configuration.DEBUG_FILE
    '"debug.log"'
    >>> configuration.NOT_DEBUGGING is None
    True
    

提交回复
热议问题