MatLab调用VC的动态链接库

使用Matlab很是方便,当然c是目前最流行的语言,所以对他们的混合编程是重要的。看到相当一部分攻略,感觉用动态库的形式是最方便的。

用Matlab调用动态库是需要头文件和动态库,并且保证导出的动态库文件名不能改变,在头文件的最后还要有换号(否则会出现NO newling的错误)

要保证导出名字不改变则需要使用*.def文件。下面用自己练习的实例展示:

vc6.0建立建立动态库工程,

在test.cpp

_declspec(dllexport) int add(int a,int b)

{

return a+b;

}

 

_declspec(dllexport) int  subtract(int a,int b)

{

return a-b;

}

或者

 int  __stdcall add(int a,int b)

{

return a+b;

}

 

int  __stdcall subtract(int a,int b)

{

return a-b;

}

或者

 int  add(int a,int b)

{

return a+b;

}

 

int   subtract(int a,int b)

{

return a-b;

}

 

 

test.def文件:

 

LIBRARY test//不是必须的可以不要

EXPORTS

add

subtract

 

test.h文件:

//函数的声明

int  add(int a,int b);

 int subtract(int a,int b);

//或者是以上三种的声明

空行//没有空行在Matlab中会出现警告

 

检测查看test.dll文件中的函数名字:

C:\Users\Administrator>d:

 

D:\>dumpbin -exports test.dll//必须加上后缀名字

Microsoft (R) COFF Binary File Dumper Version 6.00.8168

Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

 

 

Dump of file test.dll

 

File Type: DLL

 

  Section contains the following exports for test.dll

 

           0 characteristics

    513812F4 time date stamp Thu Mar 07 12:09:24 2013

        0.00 version

           1 ordinal base

           2 number of functions

           2 number of names

 

    ordinal hint RVA      name

 

          1    0 0000100A add

          2    1 00001005 subtract//后面不能出现其他的数字。

 

  Summary

 

        7000 .data

        1000 .idata

        3000 .rdata

        2000 .reloc

       2A000 .text

 

D:\>

 

cmd进入控制台,进入test.dll的目录

下面把test.dll和test.h拷贝到Matlab的工作目录下

loadlibrary('test','test.h');

x=8;y=9;

calllib('test','add',x,y);

unloadlibrary('test');

posted @ 2013-03-22 09:20  boys2012  阅读(1040)  评论(0编辑  收藏  举报