如何在D中使用C静态库?
我完全困惑于如何将 FreeType 2.4.8 编译为静态库并可在 Windows 中的 D 应用程序中使用。我尝试使用 objconv、coff2omf 运行它,并尝试 extern(C)
/extern(System)
等,但似乎没有任何效果。我收到符号未找到错误、访问冲突和一堆无用的错误。
我该如何解决这个问题?我使用 pragma lib
链接到我的静态库是否正确,以及如何可靠地将 COFF 静态库转换为 DMD/Optlink 可以使用的 OMF?
编辑:我尝试过的一些示例:
- 使用 VS 2010 将 FreeType 源编译为静态库,在我的 D 代码中使用 pragma lib 链接。 (返回“库格式未知”类型错误)
- 与上面相同,但使用 objconv 转换为 OMF 格式然后尝试与 pragma lib 链接。 (链接成功,但仍然无法调用函数。)
- 编译了 FreeType 源代码的 DLL,通过 implib 运行它以创建导入库,尝试与 pragma lib 链接。 (链接成功,但由于“尝试特权函数”或“访问冲突”错误而无法调用任何函数)
- 我尝试将函数原型定义为: extern(System)、extern(C) 和 extern(视窗)。第一个和第三个破坏了外部函数的函数名称,使得它们与静态库不匹配,第二个编译,但当我实际尝试调用这些函数时,我在运行时遇到了访问冲突。
我可以通过动态库和符号加载使其工作,但我更希望在部署项目时不需要一堆外部依赖项。
I'm completely stumped on how to get FreeType 2.4.8 compiled as a static lib and usable from within my D application in Windows. I've tried running it over with objconv, coff2omf, and trying extern(C)
/extern(System)
, etc. but nothing seems to work. I'm getting symbol not found errors, access violations and just a bunch of unhelpful errors.
How can I work around this? Am I correct in using pragma lib
to link to my static libraries, and how do I reliably convert COFF static libs to OMF which DMD/Optlink can use?
EDIT: Some examples of what I've tried doing:
- Compiling the FreeType source with VS 2010 as a static lib, linking in my D code with pragma lib. (Returned a "library format unknown" type error)
- Same as above, but converting to OMF format using objconv and then trying to link with pragma lib. (Linked successfully, but still not able to call functions.)
- Compiled a DLL of the FreeType source, ran it through implib to create an import library, tried linking with pragma lib. (Linked successfully, but unable to to call any functions due to "attempt to privileged function" or "access violation" errors)
- I've tried defining function prototypes as all of: extern(System), extern(C), and extern(Windows). The first and third mangle the functions names of the extern-ed functions such that they don't match the static libraries, and the second compiles, but I get access violations during runtime when I actually try and call the functions.
I'm able to get it working via dynamic libraries and symbol loading, but I'd much prefer to not require a bunch of external dependencies when deploying my project.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很确定 Derelict2 有 FreeType 绑定: http://www.dsource.org/projects/derelict(参见废弃FT)。
I'm pretty sure Derelict2 has FreeType bindings: http://www.dsource.org/projects/derelict (See DerelictFT).
是的,您的 .d 文件中需要带有 FT 函数声明的 extern(System)。
静态库是目标文件的集合。因此不需要任何类型的转换(omf2coff 等)。 DMD 接受静态库作为参数,因此简单的 dmd file1.d file2.d C:/path/to/freetype.lib 应该可以工作。
编辑:
我错了。显然我需要阅读一些有关 COFF2OMF 的文档。
引用: Microsoft COFF 格式显然在 Visual C++ 6.0 中发生了变化。要在较新格式的 .lib 文件上使用 coff2omf,请使用 Microsoft 的链接器将文件转换为早期的 COFF 格式:
因此,根据上面的语句判断,您需要执行 2 个步骤。
首先使用 Microsoft 的链接器将用 VisualStudio 制作的静态库(COFF 格式)转换为旧 COFF 格式,如上所述。
现在执行
coff2omf freetype.lib
将静态库转换为OMF格式。现在应该可以按照我的帖子中最初描述的方式与 DMD 一起使用。
Yes, you will need extern(System) in your .d files with FT function declarations.
Static library a collection of object files. So no need for any kind of conversion (omf2coff, etc). DMD accepts a static libraries as arguments, so simple
dmd file1.d file2.d C:/path/to/freetype.lib
should work.Edit:
I was wrong. Apparently I needed to read some documentation about the COFF2OMF.
Quote: The Microsoft COFF format apparently changed with Visual C++ 6.0. To use coff2omf on a .lib file with the newer format, use Microsoft's linker to convert the file to the earlier COFF format:
So, judging by the statement above you need to perform 2 steps.
First use the Microsoft's linker to convert static library you made with the VisualStudio (in COFF format) to the old COFF format as described above.
Now execute
coff2omf freetype.lib
to convert the static library into the OMF format.It should now be ready to be used with the DMD the way described originally in my post.