matlab2012b与VS2012混编总结(1)——将.m文件编译成dll和lib库文件

注意:最终的最终发现,matlab2012b虽然可以生成链接库等,vs2012也能调用,但是执行不了,找了好久也没找到好的解决办法,最后改用matlab2013a,它可以直接识别vs2012的编译器,不需要添加别的文件什么的,其他的和以下所写的一样。或者用matlab2010b和vs2010也可以完美匹配哦!!!

 

        折腾了两天,终于实现了我想要的结果:将.m文件通过matlab编译产生.cpp 、 .lib 和 .dll文件,方便VS2012调用,再此总结一下,方便与我遇到同样问题的朋友借鉴,我也是初学者,若有不足之处请指正哈~~~

 

下面进入正题:

 

1、前期准备:

       我用的是win7 64位系统,matlab2012b(也是64位的),VS2012。matlab2012b检测不到VS2012编译器的似乎,所以需要添加下面四个文件:

        http://yunpan.cn/QiPJMyuiLmPdv  访问密码 2fa3

       上面的网址里面是msvc110compp.bat和msvc110compp.stp文件,放在D:\Program Files\MATLAB\R2012b\bin\win64\mbuildopts文件夹下,其中D:\Program Files\MATLAB是我matlab的安装路径

        http://yunpan.cn/QiPJE56299wYb  访问密码 c932

       上面的网址里面是msvc110opts.bat和msvc110opts.stp文件,放在D:\Program Files\MATLAB\R2012b\bin\win64\mexopts文件夹下,其中D:\Program Files\MATLAB是我matlab的安装路径

 

       光有这四个文件还不够,还需要添加一个系统变量:

       右击“我的电脑”——属性——高级系统设置——高级——环境变量——系统变量,点击新建,设变量名为“path”(这个变量名我不知道会不会有什么影响,有兴趣的同学可以试验一番),变量值设置为“D:\Program Files\MATLAB\R2012b\bin\win64”(其中D:\Program Files\MATLAB是我matlab的安装路径),然后依次点击确定,重启计算机,前期工作就算完成了!!!

 

2、设置matlab编译器:

(1)在matlab命令框中执行mex -setup(-前面有空格哦!后面没有)代码和选择过程如下所示:

>> mex -setup    %%这是我们写的执行命令

 

Welcome to mex -setup.  This utility will help you set up 

a default compiler.  For a list of supported compilers, see 

http://www.mathworks.com/support/compilers/R2012b/win64.html

 

Please choose your compiler for building MEX-files:

 

Would you like mex to locate installed compilers [y]/n? n     %%这个选择n,选择y貌似也是可以的,只是没有这么多选项了,可能也会找不到我们需要的编译器

 

Select a compiler:

[1] Intel C++ 12.0 (with Microsoft Software Development Kit (SDK) linker)

[2] Intel C++ 12.0 (with Microsoft Visual C++ 2008 SP1 linker)

[3] Intel C++ 12.0 (with Microsoft Visual C++ 2010 linker)

[4] Intel C++ 11.1 (with Microsoft Visual C++ 2008 SP1 linker)

[5] Intel Visual Fortran 12 (with Microsoft Software Development Kit (SDK) linker)

[6] Intel Visual Fortran 12.0 (with Microsoft Visual C++ 2008 SP1 linker)

[7] Intel Visual Fortran 12.0 (with Microsoft Visual C++ 2008 Shell linker)

[8] Intel Visual Fortran 12.0 (with Microsoft Visual C++ 2010 linker)

[9] Intel Visual Fortran 11.1 (with Microsoft Visual C++ 2008 SP1 linker)

[10] Intel Visual Fortran 11.1 (with Microsoft Visual C++ 2008 Shell linker)

[11] Microsoft Software Development Kit (SDK) 7.1

[12] Microsoft Visual C++ 2005 SP1

[13] Microsoft Visual C++ 2008 SP1

[14] Microsoft Visual C++ 2010

[15] Microsoft Visual C++ 2012

 

[0] None

 

Compiler: 15   %%根据自己编译器的版本选择

 

Your machine has a Microsoft Visual C++ 2012 compiler located at

C:\Program Files (x86)\Microsoft Visual Studio 11.0. Do you want to use this compiler [y]/n? y    %%确认自己的编译器路径

 

Please verify your choices:

 

Compiler: Microsoft Visual C++ 2012 

Location: C:\Program Files (x86)\Microsoft Visual Studio 11.0

 

Are these correct [y]/n? y    %%确认自己的编译器路径

 

***************************************************************************

  Warning: MEX-files generated using Microsoft Visual C++ 2012 require

           that Microsoft Visual Studio 2012 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:\Users\CSC\AppData\Roaming\MathWorks\MATLAB\R2012b\mexopts.bat

From template:             D:\PROGRA~1\MATLAB\R2012b\bin\win64\mexopts\msvc110opts.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/help/techdoc/matlab_external/bsflnue-1.html

           Building with the -largeArrayDims option enables the new API.

**************************************************************************

 

(2)在matlab命令框中执行mbuild -setup(-前面有空格哦!后面没有)代码和选择过程如下所示:

>> mbuild -setup     %%这是我们写的执行命令

 

Welcome to mbuild -setup.  This utility will help you set up 

a default compiler.  For a list of supported compilers, see 

http://www.mathworks.com/support/compilers/R2012b/win64.html

 

Please choose your compiler for building shared libraries or COM components:

 

Would you like mbuild to locate installed compilers [y]/n? n     %%这个选择n,选择y貌似也是可以的,只是没有这么多选项了,可能也会找不到我们需要的编译器

 

Select a compiler:

[1] Microsoft Software Development Kit (SDK) 7.1

[2] Microsoft Visual C++ 2005 SP1

[3] Microsoft Visual C++ 2008 SP1

[4] Microsoft Visual C++ 2010

[5] Microsoft Visual C++ 2012

 

[0] None

 

Compiler: 5   %%根据自己编译器的版本选择

 

Your machine has a Microsoft Visual C++ 2012 compiler located at

C:\Program Files (x86)\Microsoft Visual Studio 11.0. Do you want to use this compiler [y]/n? y    %%确认自己的编译器路径

 

Please verify your choices:

 

Compiler: Microsoft Visual C++ 2012 

Location: C:\Program Files (x86)\Microsoft Visual Studio 11.0

 

Are these correct [y]/n? y    %%确认自己的编译器路径

 

****************************************************************************

  Warning: Applications/components generated using Microsoft Visual C++     

           2012 require that the Microsoft Visual Studio 2012 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:\Users\CSC\AppData\Roaming\MathWorks\MATLAB\R2012b\compopts.bat

From template:              D:\PROGRA~1\MATLAB\R2012b\bin\win64\mbuildopts\msvc110compp.bat

 

Done . . .    %%看到这个就再次恭喜你啦,有一个设置成功咯~~~

 

3、编写.m文件开始编译

        我编写了一个最最简单的matlab程序:

                  function C = MyAdd(A,B)
                  C = A+B;
                  end

 将其保存为MyAdd.m,保存于R盘的matlab文件下。在matlab命令框中输入:

 

 >> cd R :\matlab    %%这是将当前路径设置为MyAdd.m所在的文件夹

>> mcc -W cpplib:MyAdd -T link:lib MyAdd.m   %%这条指令是将.m文件编译成我们所需要的文件

 

          编译完之后会出现以下文件:

                   matlab2012b与VS2012混编总结(1)——将.m文件编译成dll和lib库文件

        上面的文件中有三个文件是我们需要的:MyAdd.h,MyAdd.dll和MyAdd.lib。
         上面的指令(从mcc指令集中其实也可以很容易查到这条指令的意思)中cpplib:MyAdd中的MyAdd是自己要保存的dll,lib等文件的文件名,link:lib MyAdd.m中的MyAdd.m是你的m文件名。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值