将 C++ 头文件转换为 Python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/374217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert C++ Header Files To Python
提问by williamtroup
I have a C++ header that contains #define statements, Enums and Structures. I have tried using the h2py.py script that is included with Python to no avail (except giving me the #defines converted). Any help would be greatly appreciated.
我有一个包含#define 语句、枚举和结构的 C++ 头文件。我曾尝试使用 Python 中包含的 h2py.py 脚本无济于事(除了给我转换的 #defines)。任何帮助将不胜感激。
回答by
I don't know h2py, but you may want to look at 'ctypes' and 'ctypeslib'. ctypes is included with python 2.5+, and is targeted at creating binary compatibility with c-structs.
我不知道 h2py,但你可能想看看 'ctypes' 和 'ctypeslib'。ctypes 包含在 python 2.5+ 中,旨在创建与 c-structs 的二进制兼容性。
If you add ctypeslib, you get a sub-tool called codegen, which has a 'h2xml.py' script, and a 'xml2py.py', the combination of which will auto-generate the python code you're looking for from C++ headers.
如果你添加 ctypeslib,你会得到一个名为 codegen 的子工具,它有一个“h2xml.py”脚本和一个“xml2py.py”,它们的组合将自动生成你正在从 C++ 中寻找的 python 代码标题。
ctypeslib:http://pypi.python.org/pypi/ctypeslib/0.5.4a
ctypeslib:http://pypi.python.org/pypi/ctypeslib/0.5.4a
h2xml.py will require another tool called gccxml: http://www.gccxml.org/HTML/Index.html
h2xml.py 将需要另一个名为 gccxml 的工具:http://www.gccxml.org/HTML/Index.html
it's best to check out (via CVS) the latest version of gccxml and build it yourself (actually easier done than said). The pre-packaged version is old.
最好检查(通过 CVS)最新版本的 gccxml 并自己构建它(实际上做起来比说的容易)。预先打包的版本是旧的。
回答by Fred Larson
From what I can tell, h2py.py isn't intended to convert anything other than #define macros. I did run across cppheaderparser, which might be worth a look.
据我所知,h2py.py 不打算转换 #define 宏以外的任何东西。我确实遇到了cppheaderparser,这可能值得一看。
回答by S.Lott
Where did you get the idea that h2py had anything to do with structs or enums?
您是从哪里得知 h2py 与结构或枚举有关的?
From the source
从源头
# Read #define's and translate to Python code.
# Handle #include statements.
# Handle #define macros with one argument.
The words 'enum' and 'struct' never appear in the module.
'enum' 和 'struct' 这两个词永远不会出现在模块中。