VS2012和matlab 2010b混合编程之环境配

来源:互联网 发布:windows窗口程序开发 编辑:程序博客网 时间:2024/05/17 23:46

VC调用matlab
Matlab是一个强大的数学计算/仿真工具,其内置了很多实用的现成的函数,而且我们经常也自己定义很多m函数。但在很多情况下,我们不得不使用VC编程。那么,如何在VC中利用matlab的资源呢?
环境:
Win8.1 64 bit
MATLAB R2010b 64bit
Visual Studio 2010 ultimate
混合编程环境配置:
要建立独立运行的C应用程序,系统中需要安装Matlab、Matlab编译器、C/C++编译器以及Matlab C/C++数学库函数和图形库函数。
Matlab编译器使用mbuild命令可以直接将C/C++源代码编译为独立运行程序,在这些源代码中可以使用Matlab提供的接口来调用Matlab数学库函数。
虽然生成的可执行程序可以脱离Matlab环境运行,但是它需要Matlab C/C++数学库文件或者图形库文件的支持才能运行。但如果C/C++源代码完全是按传统C/C++源代码语法规则编写,没有包含Matlab数学库和图形库的调用,则不仅可以独立与Matlab环境,也不需要那两个库函数的支持

  1. matlab 环境设置

打开matlab,在命令行状态下键入:mex -setup(x和-之间必须有空格,即setup变成紫色)
然后出现如下提示:

>> mex -setupPlease choose your compiler for building external interface (MEX) files: Would you like mex to locate installed compilers [y]/n? y(自己手动输入)默认装在C盘,其他盘可能需要选择nSelect a compiler: [1] Microsoft Visual C++ 2010 in C:\Program Files (x86)\Microsoft Visual Studio 10.0 [0] None Compiler: 1Please verify your choices: Compiler: Microsoft Visual C++ 2010  Location: C:\Program Files (x86)\Microsoft Visual Studio 10.0 Are these correct [y]/n? y***************************************************************************   Warning: MEX-files generated using Microsoft Visual C++ 2010 require            that Microsoft Visual Studio 2010 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\Administrator\AppData\Roaming\MathWorks\MATLAB\R2010b\mexopts.bat From template:              C:\PROGRA~1\MATLAB\R2010b\bin\win64\mexopts\msvc100opts.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/en/data/1-5C27B9/?solution=1-5C27B9            Building with the -largeArrayDims option enables the new API. 然后,继续在命令行状态下输入:>> mbuild -setupPlease choose your compiler for building standalone MATLAB applications: Would you like mbuild to locate installed compilers [y]/n? ySelect a compiler: [1] Microsoft Visual C++ 2010 in C:\Program Files (x86)\Microsoft Visual Studio 10.0 [0] None Compiler: 1Please verify your choices: Compiler: Microsoft Visual C++ 2010  Location: C:\Program Files (x86)\Microsoft Visual Studio 10.0 Are these correct [y]/n? y****************************************************************************   Warning: Applications/components generated using Microsoft Visual C++                 2010 require that the Microsoft Visual Studio 2010 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\Administrator\AppData\Roaming\MathWorks\MATLAB\R2010b\compopts.bat From template:              C:\PROGRA~1\MATLAB\R2010b\bin\win64\mbuildopts\msvc100compp.bat Done . . .

2.VS环境设置
(1)设置Include 和Library 目录
项目右键->属性->VC++目录->包含目录:添加
\R2010b\extern\include\win64
\R2010b\extern\include\
项目右键->属性->VC++目录->库目录:添加
\R2010b\extern\lib\win32\microsoft
\R2010b\extern\lib\win64\microsoft
截图(部分):这里写图片描述
(2)设置工程目录
项目右键->属性->C/C++->常规->附件包含目录:添加
\R2010b\extern\include\win64
\R2010b\extern\include\
项目右键->属性->链接器->常规:添加
\R2010b\extern\lib\win32\microsoft
\R2010b\extern\lib\win64\microsoft
项目右键->属性->链接器->输入->附件依赖性:添加
libeng.lib
libmat.lib
libmex.lib
libmx.lib  
注:1.lib文件包含在\R2010b\extern\lib\win64\microsoft,可根据需要增减
2.附件依赖性在VS2010前以空格隔开,在VS2010后使用;隔开。
截图: 这里写图片描述
注:附加依赖项的设置方式亦可以选择代码包含方式:

// link matlab sys lib#pragma comment(lib, "mclmcrrt.lib")#pragma comment(lib, "libmx.lib")#pragma comment(lib, "libmat.lib")#pragma comment(lib, "mclmcr.lib")

特别注意在64bit 系统下,目前我的实验结果是只能进行X64编译,即必须更改VS编译方式为x64,否则会出现错3,个人猜测在32bit系统下应该可以进行win32编译(当然matlab也是32bit)
设置方式:
项目右键->属性->链接器->高级->目标计算机:更改(只针对64位WINDOWS)
MachineX86 -> MachineX64
注:分析原因为MATLAB根据计算机平台选择了X64编译环境,如此处不修改会出现 错误3
(3)配置管理器设置(只针对64位WINDOWS)
生成->配置管理器->平台:更改
Win32->X64
注:分析原因为配合链接器中目标计算机的修改,如此处不修改会出现 错误4
截图:这里写图片描述
这里写图片描述
3.系统(Windows)环境设置
我的电脑->属性->高级系统设置->高级->环境变量->Path,添加:
\R2010b\bin;\R2010b\extern\lib\win64\microsoft;\bin\win64
注:1.Windows7的情况下,“用户变量”和“系统
2.重启后生效
变量”都需 添加,如不添加,运行时会出现 错误5
(必须设置环境变量)
附录:错误情况及解决
2.提示:“libeng.lib libmat.lib”无法打开
解决:附件依赖性在VS2010前以空格隔开,在VS2010后使用;隔开。

3.提示:“ “error LNK2019: 无法解析的外部符号 _interp1Initialize,该符号在函…””
解决:操作如2.VS环境设置中的(2) 项目右键->属性->链接器->高级->目标计算机:更改…及(3)
截图如下:
4.提示:“ “模块计算机类型“X86”与目标计算机类型“x64”冲突…””
解决:操作如2.VS环境设置中的(3)

4.提示:“ 系统丢失libeng.dll…””
解决:操作如 3.系统环境设置

参考:
http://blog.sina.com.cn/s/blog_7e2e98ad0101hd3c.html(main)
http://blog.csdn.net/u010177286/article/details/45173311
http://blog.csdn.net/zclzcl501/article/details/31792531
http://www.cnblogs.com/xpvincent/archive/2013/02/05/2893046.html
http://www.cnblogs.com/uniquews/archive/2012/12/27/2835923.html

0 0
原创粉丝点击