有没有办法包装一个像关键字一样命名的结构(例如打印)? [英] Is there a way to wrap a structure that is named like a keyword (eg. print)?

查看:38
本文介绍了有没有办法包装一个像关键字一样命名的结构(例如打印)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 C 源代码,想将它封装在 Cython 中.现在,问题是,有一个名为 print 的结构,将其外部化会引发语法错误.

I have some C source code and want to wrap it in Cython. Now, the problem is, that there is a structure called print, and externing it throws a syntax error.

cdef extern from "foo.h":
    struct print:
        # ...

当属性或函数或类似的东西像关键字一样被调用时,也会出现同样的问题.

Same problem would appear when an attribute or a function or alike is called like a keyword.

cdef extern from "foo.h":
    struct foo:
        bint print
    print(char*, int)

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

Is there a way to work around this, without modifieng the source? Maybe some technique that replaces a proxy-name with the real-name in the source-file ?

推荐答案

我认为您正在寻找的解决方案是:

I think the solution you are looking for is something along the lines of:

cdef extern from "foo.h":
    struct print "MY_print":
        double var "MY_var"

print.var 将被定义为:

print.var will then be defined by:

MY_print.MY_var

通过这种方式,您可以重命名头文件中的结构、函数、联合和枚举.当 Cython 将您的代码编译成 C 代码时,名称会被转换.

This way you can rename structs, functions, unions and enums from the header file. The names are converted when Cython compiles your code into C code.

Cython 文档的相关部分可以找到 这里.

The relevant part of Cython documentation can be found here.

这篇关于有没有办法包装一个像关键字一样命名的结构(例如打印)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆