Delphi 需要的 C 头文件翻译工具:Chet

目前来说比较好的C头文件翻译器

由于 Chet 使用实际的编译器,因此您需要安装一个(最小)C 语言开发环境,以及带有 Clang 的 LLVM。Clang 需要能找到开发环境的系统头文件。如果你安装了某种版本的 Visual Studio 和 Visual C++,通常就能找到这些头文件。免费(社区)版的 Visual Studio 就足够了。

正如 Github 上的 readme 所说,与其他一些头文件翻译器不同,Chet 使用 Clang 编译器来解析头文件,因此翻译更准确,需要的手动调整也更少。

下面是 Chet 列出的强大功能:

  1. 转换 C 数据类型,如结构体、联合体、枚举、类型定义、过程类型和不透明类型。
  2. 将 C 函数翻译为 Delphi 程序或函数。
  3. 尽可能将 #define 声明翻译为常量。
  4. 为整个 .h 文件目录生成一个 .pas 文件。这就减少了因头文件之间的依赖关系而产生的问题。
  5. 如果需要,可为多个平台(Windows、macOS、Linux、iOS 和 Android)生成输出。
  6. 你可以通过向编译器提供命令行参数来定制 Clang 解析过程。
  7. 你还可以自定义某些转换操作的输出。
  8. 根据需要保留任何 Doxygen 风格的文档注释,或将其转换为 XmlDoc 注释或 PasDoc 注释。
  9. 提供用于配置转换过程的图形用户界面,并允许将转换设置保存到 .chet 项目文件中以便重复使用。

虽然 Chet 拥有令人惊叹的功能,但也有一些用户必须注意的限制:

  1. Chet 只能处理 C 头文件,而不能处理 C++ 头文件。
  2. 头文件中的所有非内联函数都会被翻译,并被假定在项目的静态库或动态库中可用。实际情况并非如此。(内联函数永远不会被翻译,因为它们在库中永远不可用)。
  3. 由于 Clang 被用来解析头文件,这意味着 Clang 的预处理器也要运行,以执行条件解析(由 #ifdef 指令引导)。这既是好事也是坏事。说它好,是因为它提高了转换的准确性。但也有坏处,因为它会使用 Chet 运行的系统来确定某些条件路径。例如,由于 Chet 在 Windows 上运行,它将解析 #ifdef _WIN32 部分中的代码,但会跳过其他平台部分中的任何代码。

您可以先运行 Chet 检查是否有任何与缺少依赖项相关的错误。如果在运行翻译器时出现任何依赖项错误,可以在此处下载依赖项:

  • 带有 Clang 的 LLVM(建议使用 Clang for Windows(64 位));
  • Visual Studio IDE(免费社区版即可;务必安装 C++ 支持)。

你可以使用 Bin 目录中预编译的 64 位 Windows Chet 应用程序。

如果您想自己编译 Chet,则还需要 Delphi 的 libclang,并确保 Delphi IDE 可以找到它(如果 Neslib.Clang 目录与 Chet 目录位于同一级别,则 Chet 项目会自动找到它)。

Chet 采用简化 BSD 许可协议授权。

从 Github 的介绍中,我们了解到 Chet 非常简单。在许多情况下,您只需提供包含头文件的目录、输出 .pas 文件的名称,然后选择 "运行头翻译器 (F9)"。

为了更好地控制转换过程,你可以指定各种选项,具体如下。下面是 GitHub 上该项目的截图:

这无疑是最好的头文件解析器,如果你想试试,请点击下面的链接下载:

GitHub - neslib/Chet: C Header Translator for Delphi

This Zip File includes the latest version of Bob Swart‘s (aka Dr. Bob) Header Converter Utility whose purpose is to translate C header files into Delphi interface units. In an earlier release, Dr. Bob described the tool in these words: "HeadConv v4.20 is capable of generating implicit and explicit Delphi import units (16/32-bit compatible) for C DLL header files. Please note that HeadConv is not perfect (it will fail on complex structures) and on average "only" has a 85-90% correct conversion rate - there‘s always some handwork required before and after converting the header files - see also my article on Using C DLLs with Delphi at http://www.drbob42.com/delphi/headconv.htm." Several years ago Dr. Bob donated this utility to Project JEDI to aid in its header translations. Some time later Alan C. Moore, a member of Project JEDI, began the process of re-factoring the source code to improve its readability in preparation for its official release to Project JEDI. These files are the culmination of that process. Folders and Files Readme.txt this file Samples/Sample.h Sample header file to test translations Source/Headconv.dpr Main Delphi project file for building utility Source/Headpars.pas Main parsing unit for converting header files Source/Headutil.pas Utility functions used in Headpars.pas Source/Headvars.pas Global variables used in other units. Because of technical limitations we are unable to include the executable that accomplishes header translations, Bin/HeadConv.exe. This file will continue to be available at Dr. Bob‘s Website in the "tools" section at http://www.drbob42.com/tools/index.htm.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值