在matlab中使用dll

发布于 2024-08-21 16:28:18 字数 160 浏览 6 评论 0原文

我在 matlab 中使用 dll fortran 时遇到问题。 我无法在 matlab 中使用由 fortran 构建的 dll。我在 matlab 中使用“loadlibrary”指令,但错误与头文件有关。 什么是头文件?? 请给我更多信息以在 matlab 中加载 dll fortran 并调用它。

i have a problem to using a dll fortran in matlab.
i couldn't use a dll ,that is built by fortran, in matlab. i use "loadlibrary" instruction in matlab but the error is related to header files.
what is header files??
please give me more information to load a dll fortran in matlab and call it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

皇甫轩 2024-08-28 16:28:18

我建议您使用 Matlab 的 MEX 功能重新构建它,而不是尝试直接使用 dll 文件。是的,mex 文件是一个 dll,您可以在 Matlab 之外构建 dll 并成功使用它们,对于像我猜您这样的初学者来说,使用 MEX 要容易得多。一种更简单的方法是,如果您构建一个 mex 文件,系统不会要求您提供头文件,正如您所知,这对于 Fortran 程序员来说是一个相当陌生的概念。 MEX 让您的生活更轻松的另一种方式是,您可以直接从 Matlab 的命令行调用 dll 公开的函数,而无需加载库。

研究MEX文件的Matlab文档,特别注意如何以这种方式集成Fortran。

Rather than try to use a dll file directly I suggest you re-build it using Matlab's MEX functionality. Yes, a mex file is a dll and you can build dlls outside Matlab and use them successfully, it's a lot easier, for a beginner such as I guess you to be, to use MEX. One way in which it is easier is that, if you build a mex file, the system won't ask you for a header file which is, as you know, a rather foreign concept to a Fortran programmer. Another way in which MEX will make your life easier is that you can then call the function exposed by the dll directly from Matlab's command line, without loadlibrary.

Study the Matlab documentation on MEX files, pay particular attention to how to integrate Fortran this way.

南汐寒笙箫 2024-08-28 16:28:18

如果没有看到您在 MATLAB 中使用的头文件和命令行,这里很难为您提供太多帮助。您可以参考 MATLAB 中的文档,该文档要求您将两个参数传递给 loadlibrary,第二个参数是带有函数签名的头文件。我猜你没有提供第二个论点。

Without seeing your header file and the command line you're using in MATLAB, it's hard to help you too much here. You might reference the documentation in MATLAB which request that you pass two arguments to loadlibrary, the second being the header file with function signatures. I am guessing you are not providing this second argument.

蝶舞 2024-08-28 16:28:18

您需要提供一个头文件来定义您将调用的 Fortran DLL 中的每个命名函数。例如,如果您的 DLL 包含一个名为 sum 的函数,该函数对两个双精度变量求和,例如:

function sum(a,b) result(sum)
  real(kind=2), intent(in) :: a, b
  real(kind=2) :: sum
  sum = a + b
end function

那么您的标头将需要包含类似以下内容:

double sum(double*a, double*b);

但不要忘记用特定名称修饰它到你的 Fortran 编译器。例如,如果 sum 位于名为 foo 的模块中,并且您使用 gfortran 进行编译,那么您将需要类似以下内容:

double __foo_MOD_sum(double*a, double*b);

还有很多其他情况,但这就是它的要点。

You need to provide a header file that defines each of the named functions in the Fortran DLL that you'll be calling. For instance, if your DLL contains a function named sum that sums two double precision variables, like:

function sum(a,b) result(sum)
  real(kind=2), intent(in) :: a, b
  real(kind=2) :: sum
  sum = a + b
end function

Then your header will need to contain something like:

double sum(double*a, double*b);

But don't forget to decorate this with the name mangling specific to your Fortran compiler. For instance, if sum was in a module named foo, and you compiled with gfortran, then you will need something like:

double __foo_MOD_sum(double*a, double*b);

There are a lot of other cases, but that's the gist of it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文