有没有办法包装一个像关键字一样命名的结构(例如 print )?

Nik*_*s R 5 keyword cython word-wrap

我有一些C源代码,想将其包装在 Cython 中。现在的问题是,有一个名为 的结构print,并且 externing 它会引发语法错误。

cdef extern from "foo.h":
    struct print:
        # ...
Run Code Online (Sandbox Code Playgroud)

当像关键字一样调用属性或函数等时,也会出现同样的问题。

cdef extern from "foo.h":
    struct foo:
        bint print
    print(char*, int)
Run Code Online (Sandbox Code Playgroud)

有没有办法在不修改源代码的情况下解决这个问题?也许有某种技术可以用源文件中的真实姓名替换代理名称?

Sim*_*mon 5

我认为您正在寻找的解决方案类似于:

cdef extern from "foo.h":
    struct print "MY_print":
        double var "MY_var"
Run Code Online (Sandbox Code Playgroud)

print.var 将被定义为:

MY_print.MY_var
Run Code Online (Sandbox Code Playgroud)

这样您就可以从头文件中重命名结构、函数、联合和枚举。当 Cython 将代码编译为 C 代码时,名称会被转换。

Cython 文档的相关部分可以在此处找到。