是否可以将C预处理器宏可靠地转换为python代码? [英] Is it possible to reliably convert C preprocessor macros to python code?
问题描述
我有一堆 C 宏,我需要在python中对其进行仿真。我看到了一些指向pygccxml或ctypeslib等的指针。这是要走的路吗?还是有更好的东西?
I have a bunch of C macros the operation of which I need to simulate in python. I saw some pointers to pygccxml or ctypeslib etc. Are these the ways to go? Or is there something out there that is better ?
C 宏,如果它们更改时,我希望python实现是自动的生成,而不必进行手动修改。因此就是问题。
The C macros if and when they change, I would like the python implementation to be auto generated rather than having to make manual modifications. Hence the question.
my_c_header.h :
#ifdef OS
#define NUM_FLAGS (uint16_t)(3)
#define NUM_BITS (uint16_t)(8)
#else
#define NUM_FLAGS (uint16_t)(6)
#define NUM_BITS (uint16_t)(16)
#endif
#define MAKE_SUB_FLAGS (uint16_t)((1<<NUMFLAGS) -1)
#define MAKE_TOTAL_FLAGS(x) (uint16_t)((x & MAKE_SUB_FLAGS) >> NUM_BITS)
/* #defines type 2 */
#ifdef OS
#DO_SOMETHING(X) os_specifc_process(x)
#else
#DO_SOMETHING(x)
#endif
/* #defines type 3 */
enum
{
CASE0,
CASE1,
CASE2
}
#define MY_CASE_0 ((uint16_t)CASE0)
#define MY_CASE_1 ((uint16_t)CASE1)
#define MY_CASE_2 ((uint16_t)CASE2)
/*End of file <my_c_header.h> */
推荐答案
如果要编写扩展模块,请使用 https://docs.python.org/3/c- api / module.html#c.PyModule_AddIntMacro
If you are writing an extension module, use https://docs.python.org/3/c-api/module.html#c.PyModule_AddIntMacro
这篇关于是否可以将C预处理器宏可靠地转换为python代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!