`
ai937ai
  • 浏览: 19400 次
社区版块
存档分类
最新评论

Fortran调用Matlab

 
阅读更多

Fortran调用Matlab
2011年12月27日
  【试验环境】Windows 2003 server std,matlab r2007b,MinGW gcc 4.2 (sjlj)。Visual Studio 2005
  【试验任务】实现在fortran程序中调用matlab编译生成的DLL。
  【试验步骤】首先,创建一个简单的m代码,这里用拙作《深入浅出matlab7.x混合编程》中用到的mcctest01.m作为例子,它是一个很简单的m方程,程序代码如下:
  代码:function mcctest01
  
% MCCTEST01 Create some data and draw a 2D x-y plot
  %   mcctest01 is used to demonstrates how to convert M-functions into  
  %   library files and how to use created library files with MFC
  %   applications.
  %
  %   Inputs:
  %       None
  %  
  %   Output:
  %       None
  %
  %   Copyright (c) 2005 Dong Weiguo
  % create data
  x = 0:.01:20;
  y = x.*sin(x);
  % draw a plot
  plot(x,y);
  xlabel('x');
  ylabel('y');
  然后在matlab中编译mcctest01,生成libmcctest01.dll。注意,如果这是你第一次在matlab中使用mcc命令,你需要先运行mbuild -setup来配置。我使用Visual Studio 2005编译器。在matlab中执行如下命令:
  mcc -W cpplib:libmcctest01 -T link:lib mcctest01.m
  这里假设matlab中的当前工作环境是$MATLAB\work。结果将会产生若干文件,我们需要拷贝如下四个文件到我们的Fortran源程序目录
  libmcctest01.h
  libmcctest01.lib
  libmcctest01.dll
  libmcctest01.ctf
  接下来的任务就是如何在fortran程序中调用libmcctest01.dll
  Fortran无法直接使用动态链接库(或者有些版本的fortran编译器,比如PGI,或Visual Fortran等有相关的扩展,不过我不了解)。长话短说,在fortran程序中,我们可以通过调用c语言的方式间接调用dll,具体的做法是我们为调用dll中的函数,另外在写一个和fortran兼容的c或c++语言的wrapper函数。这里牵涉到fortran和c/c++的混合编程,主要有两点需要注意的:
  1. Fortran程序中函数的参数是以地址的方式传递的。也就是说类似于
  subroutine foo(x, y, z)
  real x, y, z
  其等效的C语言函数声明则类似
  void foo( float* x, float* y, float* z);
  2. 有些fortran编译器比如g77,在编译函数后会在函数名的后面在多加一个下划线“_",也就是上面的subroutine foo编译后,函数名就变成了foo_。换句话说,如果在fortran中用
  call foo(...)
  调用foo子函数,其实是找foo_。
  下面给出针对于我们的例子,也就是libmcctest01.dll中mcctest01函数的调用,一个可行的C++语言的wrapper函数
  // File: mcctest01wrap.cpp
  // Description: A simple testing c++ souce file. Two functions
  // are defined, which will be called in a FORTRAN 77 code.
  //
  // History: Created on 16 January, 2006
  // Created by: Dong Weiguo
  //////////////////////////////////////////////////////////////
  #include "libmcctest01.h"
  // By default, most of fortran compilers would add an
  // undrescore at the end of function name
  // So let's emulate this bahavior
  extern "C" void mymccfunc_()
  {
  libmcctest01Initialize();
  mlxMcctest01(0, NULL, 0, NULL);
  std::cout
  
  ress any key to quit!" 这个例子的fortran主程序
  !      A test program to demonstrate how to call c++
  !      functions from a Fortran program.
  !      Synopsis: f77cpptst
  !      History: 16 January, 2006
  !      Created by: Dong Weiguo
  program f77cpptst
  call mymccfunc()
  
       stop
  end
  可以看出,程序非常简单,就是调用wrapper函数。
  *************************************************
  *        *
  * 一、Visual Fortran中使用Matlab引擎   *
  *        *
  *************************************************
  source:http://www.simwe.com/cgi-bin/ut/topic_show.cgi?id=16634&h=1&bpg=1&age=-1
  #1.安装
  软件版本:
  Compaq Visual Fortran(以下简称CVF)6.5
  Matlab 6.1
  安装路径:
  CVF: C:\Program Files\Microsoft Visual Studio
  Matlab: D:\matlab
  #2.配置mex
  在matlab下运行命令mex
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics