vs2008调用matlab dll

实验环境:

xp sp3

MATLAB 2009b(安装路径:D:\Program Files\MATLAB\R2009a)

VS2008 中文版(安装路径:D:\Program Files\Microsoft Visual Studio 9.0)

1:在matlab中选择compiler。

在命令行窗口输入:mex -setup

 mex -setup
Please choose your compiler for building external interface (MEX) files:
 
Would you like mex to locate installed compilers [y]/n? n
 
Select a compiler:
[1] Intel C++ 9.1 (with Microsoft Visual C++ 2005 SP1 linker)
[2] Intel Visual Fortran 10.1 (with Microsoft Visual C++ 2005 SP1 linker)
[3] Intel Visual Fortran 9.1 (with Microsoft Visual C++ 2005 SP1 linker)
[4] Lcc-win32 C 2.4.1
[5] Microsoft Visual C++ 6.0
[6] Microsoft Visual C++ .NET 2003
[7] Microsoft Visual C++ 2005 SP1
[8] Microsoft Visual C++ 2008 Express
[9] Microsoft Visual C++ 2008 SP1
[10] Open WATCOM C++
 
[0] None
 
Compiler: 9
 
The default location for Microsoft Visual C++ 2008 SP1 compilers is C:\Program Files\Microsoft Visual Studio 9.0,
but that directory does not exist on this machine. 
 
Use C:\Program Files\Microsoft Visual Studio 9.0 anyway [y]/n? n
Please enter the location of your compiler: [C:\Program Files\Microsoft Visual Studio 9.0] D:\Program Files\Microsoft Visual Studio 9.0
 
Please verify your choices:
 
Compiler: Microsoft Visual C++ 2008 SP1 
Location: D:\Program Files\Microsoft Visual Studio 9.0
 
Are these correct [y]/n? y
 
***************************************************************************
  Warning: MEX-files generated using Microsoft Visual C++ 2008 require
           that Microsoft Visual Studio 2008 run-time libraries be 
           available on the computer they are run on.
           If you plan to redistribute your MEX-files to other MATLAB
           users, be sure that they have the run-time libraries.
***************************************************************************
 
Trying to update options file: C:\Documents and Settings\chenwq\Application Data\MathWorks\MATLAB\R2009a\mexopts.bat
From template:              D:\PROGRA~1\MATLAB\R2009a\bin\win32\mexopts\msvc90opts.bat
 
Done . . .
 
**************************************************************************
  Warning: The MATLAB C and Fortran API has changed to support MATLAB
           variables with more than 2^32-1 elements.  In the near future
           you will be required to update your code to utilize the new
           API. You can find more information about this at:
           http://www.mathworks.com/support/solutions/data/1-5C27B9.html?solution=1-5C27B9
           Building with the -largeArrayDims option enables the new API.
**************************************************************************

以上xxxxx部分为键盘输入的内容

2:选择builder

在命令行窗口输入:mbuilder -setup

与上述步骤类似,最后输出:

****************************************************************************
  Warning: Applications/components generated using Microsoft Visual Studio  
           2008 require that the Microsoft Visual Studio 2008 run-time      
           libraries be available on the computer used for deployment.      
           To redistribute your applications/components, be sure that the   
           deployment machine has these run-time libraries.                 
****************************************************************************
 
Trying to update options file: C:\Documents and Settings\chenwq\Application Data\MathWorks\MATLAB\R2009a\compopts.bat
From template:              D:\PROGRA~1\MATLAB\R2009a\bin\win32\mbuildopts\msvc90compp.bat
 
Done . . .

3:将test1.m文件编译生成所需的 .dll .lib .h文件 

%/*********************** test1.m *************************/
function output = test1(input)
%%

%
%%
output=input.^0.5;
plot(input,output);
axis equal;
return
end

输入:mcc -W cpplib:test1dll -T link:lib test1.m 
生成文件如下:

 

4.新建win32控制台程序,并配置

新建一个win32 控制台应用程序,我取的名字是CallMatDll.当然新建其他的项目类型也可以,我这只是个例子。接下来进行配置,在该项目的属性中进行了配置,只对该项目有效。若建新的项目需要重新配置。项目建好后将test1dll.lib, test1dll.h, test1dll.dll 拷贝到项目目录下。

配置项目属性页/配置属性/C-C++/常规/附加包含目录:

配置项目属性页/配置属性/链接器/常规/附加库目录:

配置项目属性页/配置属性/链接器/输入/附加依赖性,填入test1dll.lib mclmcrrt.lib mclmcr.lib

5.主程序,调试运行

 

#include "mclmcrrt.h"
#include "mclcppclass.h"
#include <iostream>
#include "test1dll.h"
using namespace std;

int main( )
{    
    cout << "Hello world!" << endl;
    /* Initialize the MCR */ 
    if( !mclInitializeApplication(NULL,0) ) 
    { 
            cout << "Could not initialize the application!" << endl;
            return -1; 
    } 

    
    // initialize lib
    if( !test1dllInitialize())
    {
            cout << "Could not initialize libmyadd2!" << endl;
            return -1; 
    }

    // declare and initialize a
    mwArray a(1, 4,  mxDOUBLE_CLASS);

    double *aData;
    aData = new double[4];
    int i;
    for( i=0; i<4; ++i)
    {
            aData[i] = 1.0*i;
    }
    // print output
    std::cout << "a = " << std::endl;
    std::cout << aData[0] << ",\t" << aData[1] <<",\t"<< aData[2] << ",\t" << aData[3] << std::endl;


    a.SetData(aData, 4);

  
    mwArray y(1, 4,  mxDOUBLE_CLASS);
    // call the function
    test1(1, y, a);
    

    // copy data from mwArray to C++ objects

    // allocate outputs
    double *yData;
    yData = new double[4];
    if( yData == NULL )
    {
            std::cout << "Failed to allocate memory for yData!" << std::endl;
            return -1;
    }

 
    // copy data from mwArray to C++
    y.GetData(yData, 4);
    

    // print output
    std::cout << "y = " << std::endl;
    std::cout << yData[0] << ",\t" << yData[1] <<",\t"<< yData[2] << ",\t" << yData[3] << std::endl;

  
    
    // deallocate memory
    delete [] aData;
    delete [] yData;

    /* Block on open figures */
    mclWaitForFiguresToDie(NULL);
   
    // terminate the lib
    test1dllTerminate();
    

    // terminate MCR
    mclTerminateApplication();


    return 0;
}

 

编译运行结果如下:

 

 

参考:

http://blog.csdn.net/infocarrier/article/details/5854522

http://blog.csdn.net/hahalxp/article/details/5415257

http://www.cnblogs.com/EverYoung/archive/2012/03/29/2423418.html

 

转载于:https://www.cnblogs.com/vicchen/archive/2013/01/11/2856340.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值