我在 Windows XP 上使用 Delphi(D7 和 D2006)进行编程(不久的将来将迁移到 Windows 7)。我需要使用数学库来完成我正在做的一些工作,并且我看过的大多数数学库(我目前倾向于 Mathematica)都会生成编译后的 C 代码。此类代码将为我的主程序提供特定功能。
我有一个非常基本的问题 - 考虑到这个开发设置 - 我如何开始利用 Delphi 编译的 C 代码?我真的需要一些小步骤来让我开始这个过程。
I program in Delphi (D7 and D2006) on Windows XP (migrating in the near future to Windows 7). I need to use a mathematical library for some of the work I am doing and most of the math libraries (I am inclining towards Mathematica at present) I have looked at will produce compiled C code. Such code will provide specific functionality to my main programs.
I have a very basic question - given this development setup - how do I start utilising the compiled c code from Delphi? I really need baby steps to get me started on the process.
发布评论
评论(4)
如果您当前安装了 Mathematica,请转至文档中心并查找
guide/CLanguageInterface
,否则该指南为 可在网络上找到,并在那里好好阅读。我的理解是,如果您需要完整的功能,或者如果您只需要较低级别的功能,Mathematica 可以生成通过 MathLink 与 Mathematica 引擎链接的 C 程序,那么它能够生成可以与编译的 Mathematica 库静态链接的代码。因此独立代码是可能的。
请参阅代码生成器文档。
如果您可以将 C 程序转换为 DLL,那么从 Delphi 访问此类外部函数就是相对简单的外部声明。
如果您需要实现静态绑定,以便在未安装 Mathematica 的情况下使用(如果确实可能的话),那么使其工作肯定会非常复杂。我从来没有尝试过。
If you currently have Mathematica installed, go to the documentation centre and lookup
guide/CLanguageInterface
otherwise that guide is available on the web and have a good read there.My understanding is that Mathematica can generate C-programs that link up with the Mathematica engine via MathLink if you need full function, or if you only need lower-level features then it is capable of generating code that can be statically linked with compiled Mathematica libraries. So that standalone code is possible.
See the Code Generator documentation.
If you can convert the C programs in to DLLs, then accessing such external functions from Delphi is relatively simple with external declarations.
There are bound to be a great number of complexities in getting this to work if you need to achieve a static bind, for use where Mathematica is not installed, if indeed it is possible. I have never attempted it.
您可以使用 RAD Studio 将您的项目与 Delphi 和 C++ (Builder) 代码混合。将自动创建的 C 代码放入 C++ Builder 文件 (.cpp) 中,其余部分添加 Delphi 文件。
You can mix your project with Delphi and C++ (Builder) code using RAD Studio. Put the automatically created C code into a C++ Builder file (.cpp) and for the rest add Delphi files.
我对 Lohninger 的 SDL 库非常满意 (http://www.lohninger.com/mathpack.html )。它是用 Delphi 编写的,并直接编译到您的应用程序中,因此不存在捆绑或调用约定问题或浮点使用差异,正如本线程中其他响应所讨论的那样。
看看他包含了什么。如果你幸运的话,他的图书馆会满足你的需求,你就可以使用它了!
I have been very happy with the SDL Library from Lohninger (http://www.lohninger.com/mathpack.html). It is written in Delphi and compiles right into your application, so there are no bundling or calling convention problems or floating point usage differences, as discussed by other responses in this thread.
Take a look at what he includes. If you're lucky, your needs will be met by his library and you'll be able to use it!
我已经用我的 FE 产品 OrcaFlex 完成了相当多的工作。您有两种选择从 Delphi 链接到 C 代码:静态或动态。我静态链接是因为它使分发和版本控制变得更加容易。但要让它静态工作确实是一个很大的技巧,并且您必须依赖 Delphi 的许多未记录的方面。
我怀疑对于您的需求,动态链接是最好的。基本上,您需要将 C 代码编译并链接到 DLL 中。我建议使用 Borland C 编译器来执行此操作。您可以使用免费命令行版本 BCC55 来执行此操作。使用 Borland C 的优点是它对 8087 浮点单元做出与 Delphi 相同的假设。如果您使用 MSVC 进行构建,那么您会发现 MS 选择不引发浮点异常。 Borland C 确实引发浮点异常。这是一个有点特殊的情况,但如果您试图发布需要强大的产品,那么它就变得相关了。
您应该知道,默认情况下,C 代码将使用 C 调用约定,我将坚持这一点。您可以通过将外部例程声明为 cdecl 调用约定将其引入 Delphi。
您需要注意的另一件事是在两个模块之间定义清晰的接口。您需要确保异常不会跨越模块边界,并且不会跨边界传递任何特殊类型(例如 Delphi 字符串)。因此,对于字符串,请使用 PChar(或者更好的 PAnsiChar 或 PWideChar,以确保升级到 Delphi 2009 及更高版本时它的含义不会改变)。
I've done quite a bit of this with my FE product OrcaFlex. You have two options to link to your C code from Delphi: static or dynamic. I link statically because it makes distribution and versioning much easier. But it's really quite a trick to get it to work statically and you have to rely on a number of undocumented aspects of Delphi.
I suspect that for your needs dynamic linking is best. Basically you need to compile and link your C code into a DLL. I recommend using the Borland C compiler to do this. You can use the free command line version BCC55 to do this. The advantage of using Borland C is that it makes the same assumptions about the 8087 floating point unit as Delphi does. If you build with MSVC then you will find that MS have elected not to raise floating point exceptions. Borland C does raise floating point exceptions. This is a bit of a corner case but it becomes relevant if you are trying to ship a product that you need to be robust.
You should know that the C code will, by default, use the C calling convention and I'd just stick with that. You bring it into Delphi by declaring the external routine as cdecl calling convention.
The other thing you need to take care on is defining a clear interface between the two modules. You need to make sure that exceptions don't cross the module boundary and that you don't pass any special types (e.g. Delphi strings) across the boundary. So for a string use a PChar (or even better PAnsiChar or PWideChar to be sure that it won't change meaning when you upgrade to Delphi 2009 and later).