注意:最终的最终发现,matlab2012b虽然可以生成链接库等,vs2012也能调用,但是执行不了,找了好久也没找到好的解决办法,最后改用matlab2013a,它可以直接识别vs2012的编译器,不需要添加别的文件什么的,其他的和以下所写的一样。或者用matlab2010b和vs2010也可以完美匹配哦!!!
下面进入正题:
1、前期准备:
2、设置matlab编译器:
(1)在matlab命令框中执行mex -setup(-前面有空格哦!后面没有)代码和选择过程如下所示:
>> mex -setup
Welcome to mex -setup.
a default compiler.
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
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
***************************************************************************
***************************************************************************
Trying to update options file: C:\Users\CSC\AppData\Roaming\MathWorks\MATLAB\R2012b\mexopts.bat
From template:
Done . . .
**************************************************************************
**************************************************************************
(2)在matlab命令框中执行mbuild -setup(-前面有空格哦!后面没有)代码和选择过程如下所示:
>> mbuild -setup
Welcome to mbuild -setup.
a default compiler.
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
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
****************************************************************************
****************************************************************************
Trying to update options file: C:\Users\CSC\AppData\Roaming\MathWorks\MATLAB\R2012b\compopts.bat
From template:
Done . . .
3、编写.m文件开始编译
>> mcc -W cpplib:MyAdd -T link:lib MyAdd.m