The Combined Programming of VS2008 and Matlab

简介: Today, let’s do one thing: take a combined programming with VS2008 and matlab2009a.   Configuration of Matlab First, launch matlab and do the following to configure the initial environment :(th

Today, let’s do one thing: take a combined programming with VS2008 and matlab2009a.

 

Configuration of Matlab

First, launch matlab and do the following to configure the initial environment :(the red part is my input)

mbuild-setup

Please choose yourcompiler for building standalone MATLAB applications:

Would you like mbuild to locate installed compilers [y]/n?y

Select a compiler:

[1] Lcc-win32 C2.4.1 in C:\PROGRA~1\MATLAB\R2009a\sys\lcc

[0] None

Compiler: 0

  mbuild: No compiler selected. No actiontaken.

 

mbuild -setup

Please choose your compiler forbuilding standalone MATLAB applications:

Would you like mbuild to locateinstalled compilers [y]/n?n

Select a compiler:

[1] Lcc-win32 C 2.4.1

[2] Microsoft Visual C++ 6.0

[3] Microsoft Visual C++ .NET 2003

[4] Microsoft Visual C++ 2005 SP1

[5] Microsoft Visual C++ 2008Express

[6] Microsoft Visual C++ 2008 SP1

[0] None

 

Compiler: 6(make sure your vs2008 is the official version, not the express)

 

The default location for Microsoft Visual C++ 2008 compilers is C:\Program Files\Microsoft Visual Studio9.0,

but that directory does not existon this machine. 

Use C:\Program Files\MicrosoftVisual Studio 9.0 anyway [y]/n? y

Please verify your choices:

Compiler: Microsoft Visual C++ 2008

Location: C:\ProgramFiles\Microsoft Visual Studio 9.0

 

Are these correct [y]/n?y

 

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

 Warning: Applications/components generated using Microsoft VisualC++     

           2008 require that the MicrosoftVisual Studio 2008 run-time      

           libraries be available on thecomputer used for deployment.      

           To redistribute yourapplications/components, be sure that the   

           deployment machine has theserun-time libraries.                 

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

 

Trying to update options file:C:\Documents and Settings\Administrator\ApplicationData\MathWorks\MATLAB\R2009a\compopts.bat

From template:             C:\PROGRA~1\MATLAB\R2009a\bin\win32\mbuildopts\msvc90freecompp.bat

 

Done . . .

 

second, create a m file, naming it “add.m”. Here are in add.m:

 

function y = add(a,b)

y = a + b;

end

 

third, type this in matlab command prompt:

mcc –W cpplib:myadd–T link:lib add.m –C(concrete parameters in HELP document)

One or two minutes later, there are several new files in your current directory, just like:

 

Please notice: the four files myadd.ctf, myadd.h, myadd.lib, myadd.dll should be used by vs2008 project.So you must copy them to your current vs project directory.

 

You may feel strange to file myadd.ctf. However, if you do not copy it, you’ll get this in executing stage if other operations are OK:

 


By the way, By typing the following command at VS2008 command prompt, we can get the exports and imports of myadd.dll.Please notice the red part, because we're gonna use it in our own code.

>> dumpbin myadd.dll /exports > exports.txt

here is the exports.txt:

Microsoft (R) COFF/PE Dumper Version 9.00.21022.08
Copyright (C) Microsoft Corporation.  All rights reserved.
Dump of file myadd.dll

File Type: DLL

  Section contains the following exports for myadd.dll

    00000000 characteristics
    50F6AA97 time date stamp Wed Jan 16 21:26:47 2013
        0.00 version
           1 ordinal base
           7 number of functions
           7 number of names

    ordinal hint RVA      name

          1    0 000018D0 ?add@@YAXHAAVmwArray@@ABV1@1@Z
          2    1 00001560 mlxAdd
          3    2 000014E0 myaddGetMcrID
          4    3 000014A0
myaddInitialize
          5    4 00001440 myaddInitializeWithHandlers
          6    5 000014F0 myaddPrintStackTrace
          7    6 000014C0 myaddTerminate
  Summary
        2000 .data
        4000 .rdata
        2000 .reloc
        9000 .text

>> dumpbin myadd.dll /imports > imports.txt

here is the imports.txt:

Microsoft (R) COFF/PE Dumper Version 9.00.21022.08
Copyright (C) Microsoft Corporation.  All rights reserved.

Dump of file myadd.dll
File Type: DLL
  Section contains the following imports:
   
KERNEL32.dll
              1000A000 Import Address Table
              1000C8D8 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference


                  1F4 GetModuleFileNameA
                  1F6 GetModuleHandleA
                  2A6 HeapSize
                  2E3 LCMapStringW
                  2E1 LCMapStringA
                  240 GetStringTypeW
                  31A MultiByteToWideChar
                  23D GetStringTypeA
                  1E8 GetLocaleInfoA
                  2B5 InitializeCriticalSectionAndSpinCount
                  …… many functions!

    mclmcrrt710.dll
              1000A0F4 Import Address Table
              1000C9CC Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference


                  12C mclcppGetArrayBuffer_proxy
                    A array_buffer_set_proxy
                    6 array_buffer_add_proxy
                  12A mclcppFeval_proxy
                    8 array_buffer_get_proxy
                    C array_buffer_to_cell_proxy
                   A9 mclFeval_proxy
                   DF mclGetStackTrace_proxy
                   AC mclFreeStackTrace_proxy
                   BC mclGetID_proxy
                  11F mclTerminateInstance_proxy
                  131 mclmcrInitialize_proxy
                   EC mclInitializeComponentInstance_proxy
                  126 mclWrite_proxy
                   4C array_ref_getV_int_proxy
                   66 array_ref_number_of_elements_proxy
                   44 array_ref_classID_proxy
                  12D mclcppGetLastError_proxy
                   81 error_info_get_message_proxy
                  1C3 ref_count_obj_release_proxy
                  1C2 ref_count_obj_addref_proxy
                  128 mclcppCreateError_proxy
  Summary
        2000 .data
        4000 .rdata
        2000 .reloc
        9000 .text


Configuration of VS2008

 

create a MFC project based on dialog named myadd, then do the following to contain “include and lib”:

 choose tool-option and add the matlab’s include directory. see below:

 

choose tool-option and add the matlab’s lib directory.see below:

 

Then you’re expected to enter menu project-myadd property-linker-input, do these:

 

so myadd.lib was generated just now, and the mclmcrrt.lib is necessary for myadd.dll.

The configuration of vs is completed. let’s enter the code process.

 

Implementation of basic function

 

open your myadd project and add some controller such as the static text, the edit control and button. Just like this:

 

double click the calculate button and automatically enter the message-handling function void CMyAddTestDlg::OnBnClickedOk().

 

edit it like this:

void CMyAddTestDlg::OnBnClickedOk()
{
  // TODO: 在此添加控件通知处理程序代码
  CString str;
  inta,b,c;
 
//get what’s in the edit control and save to a,b
  a=GetDlgItemInt(IDC_EDIT1);
  b=GetDlgItemInt(IDC_EDIT2);
 
//initializing function to avoid failure
  if(!myaddInitialize())
  {
     str.Format(_T("lib myadd2 Initialize failed!\n"));
     MessageBox(str);
     return;
  }
  try
  {
//declare a mwArray variable and store a to it
     mwArray mwa(1,1,mxUINT32_CLASS);
     mwa.SetData(&a,1);
 
//declare a mwArray variable and store b to it
     mwArray mwb(1,1,mxUINT32_CLASS);
     mwb.SetData(&b,1);
 
//declare a mwArray variable and store the result
     mwArray mwy(1,1,mxUINT32_CLASS);
 
//call the add function and store mwy to c
     add(1,mwy,mwa,mwb);
     mwy.GetData(&c,1);
 
     SetDlgItemInt(IDC_EDIT3,c);
  }
  catch(mwException&e)
  {
     str.Format(_T("%s"),e.what());
     MessageBox(str);
  }
 
//the ending function
  myaddTerminate();
 
  //OnOK();youshould add an annotation of it
}

And do not forget add the include file “myadd.h” to MyAddDlg.cpp !

 

So you are seeming to be allowed to link and run the program. If you do that, you may become a little disappointed. Because you’re likely to get the following error:

 

the way to it is simple.open targetver.h and modify two lines:

 

that’s to say,change the two “0600” to “0501”.

 

Then you try to run the application. To your disappointment again, you’ll get this:

 

For this problem,you may refer to this:

http://www.mathworks.ch/matlabcentral/newsreader/view_thread/249680

 

Here is my way:

find the file \MATLAB\R2009a\toolbox\compiler\deploy\win32\MCRInstaller.exe and install it. when it’s done, the installing directory will have that .dll file.In order to find it in executing stage, you should set your PATH environment variable.See below:

 

And this time,believe me ,you will get a satisfying answer if your quality is not so bad…Just see below


Congratulations !!!

目录
相关文章
|
2月前
|
存储 数据采集 数据可视化
|
2月前
|
机器学习/深度学习 数据安全/隐私保护 计算机视觉
matlab学习(二)
matlab学习(二)
|
2月前
|
计算机视觉
matlab学习(一)(3.26-4.1)
matlab学习(一)(3.26-4.1)
|
2月前
|
算法 数据安全/隐私保护
matlab学习(三)
matlab学习(三)
|
8月前
专题一MATLAB基础知识——汇总
专题一MATLAB基础知识——汇总
31 0
|
8月前
专题一MATLAB基础知识——1.6MATLAB基本运算
专题一MATLAB基础知识——1.6MATLAB基本运算
59 0
|
10月前
|
存储
第1章 MATLAB R2020a概述——1.6 MATLAB帮助系统
第1章 MATLAB R2020a概述——1.6 MATLAB帮助系统
|
10月前
第1章 MATLAB R2020a概述——1.7 初步使用MATLAB
第1章 MATLAB R2020a概述——1.7 初步使用MATLAB
|
存储 人工智能 索引
【Matlab作业】MATLAB语言基础
【Matlab作业】MATLAB语言基础
MATLAB-基础知识总结
MATLAB-基础知识总结
170 0

热门文章

最新文章

  • 1
    流量控制系统,用正则表达式提取汉字
    25
  • 2
    Redis09-----List类型,有序,元素可以重复,插入和删除快,查询速度一般,一般保存一些有顺序的数据,如朋友圈点赞列表,评论列表等,LPUSH user 1 2 3可以一个一个推
    26
  • 3
    Redis08命令-Hash类型,也叫散列,其中value是一个无序字典,类似于java的HashMap结构,Hash结构可以将对象中的每个字段独立存储,可以针对每字段做CRUD
    25
  • 4
    Redis07命令-String类型字符串,不管是哪种格式,底层都是字节数组形式存储的,最大空间不超过512m,SET添加,MSET批量添加,INCRBY age 2可以,MSET,INCRSETEX
    27
  • 5
    S外部函数可以访问函数内部的变量的闭包-闭包最简单的用不了,闭包是内层函数+外层函数的变量,简称为函数套函数,外部函数可以访问函数内部的变量,存在函数套函数
    23
  • 6
    Redis06-Redis常用的命令,模糊的搜索查询往往会对服务器产生很大的压力,MSET k1 v1 k2 v2 k3 v3 添加,DEL是删除的意思,EXISTS age 可以用来查询是否有存在1
    30
  • 7
    Redis05数据结构介绍,数据结构介绍,官方网站中看到
    21
  • 8
    JS字符串数据类型转换,字符串如何转成变量,+号只要有一个是字符串,就会把另外一个转成字符串,- * / 都会把数据转成数字类型,数字型控制台是蓝色,字符型控制台是黑色,
    19
  • 9
    JS数组操作---删除,arr.pop()方法从数组中删除最后一个元素,并返回该元素的值,arr.shift() 删除第一个值,arr.splice()方法,删除指定元素,arr.splice,从第一
    19
  • 10
    定义好变量,${age}模版字符串,对象可以放null,检验数据类型console.log(typeof str)
    19