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 :(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 !!!

时间: 2025-01-30 04:04:08

The Combined Programming of VS2008 and Matlab的相关文章

2013年5月编程语言排行榜:UNIX下的Bash

2013年5月9日,Tiobe公司发布新一期编程语言排行榜.新一期榜单前10位没有太多的变化,只是Objective-C与C++,Ruby与JavsScript在互相交换位置罢了.今天我们要关注的是排在TOP 20后半部的一门语言--Bash. 大家先请看本期TOP20榜单 从2013年4月的编程语言排行榜我们惊异的发现Bash这门UNIX下的壳语言,竟然有了飞速的上升.从第34位最高上升到第13位.究竟这门语言有什么独特之处? Bash语言2013年以来的增长势头 Bash的诞生 Bash这个

Linus,一生只为寻找欢笑

每个人桌面上一台电脑,这曾经是无数计算机先驱的梦想,这个梦想很早就实现了,在1997年,乔老师和比老师就说过,「比尔,我们共同控制了100%的桌面系统市场」,当然乔老师没说的是,比老师控制了97%,乔老师还不到3%.时至今日,乔老师走了,比老师颓了,移动终端把传统的 PC 市场冲击的七零八落.普通用户都知道了Windows.Android.OS X .iOS.BlackBerry等等,但是,他们依然不了解的是另一款在计算机发展史上起到了革命性作用的操作系统:Linux! 当大家使用 Google

《C语言开发从入门到精通》一第1章 C语言之定位1.1 C语言的诞生

第1章 C语言之定位 C语言开发从入门到精通 C语言是当前所有开发技术中使用较为广泛的一门语言,从它诞生之日起就深受程序员们的喜爱.随着C语言的普及,使得后来的开发语言都或多或少借鉴或遵循了它的一些模式.另外,C语言是计算机编程领域中最早的高级语言之一,它的出现推动了软件行业的迅猛发展.本章将简要介绍C语言的基本知识,为读者步入后面的学习打下基础. 本章内容 C语言的辉煌诞生 第一印象的建立 理解编译系统--学习的第一步 揭开存储器的层次 技术解惑 学习C语言还有用吗 怎样学好C语言 学好C语言

《C语言程序设计与实践(第2版)》——1.4 C语言的发展历史、现状与特点

1.4 C语言的发展历史.现状与特点 1.4.1 C语言的发展历史和现状 C语言的发展历史可以追溯到1961年的ALGOL 60,它是C语言的祖先.ALGOL 60是一种面向问题的高级语言,与计算机硬件的距离比较远,不适合用来编写系统软件.1963年,英国剑桥大学推出了CPL(Combined Programming Language).CPL对ALGOL 60进行了改造,在ALGOL 60基础上接近硬件一些,但是规模较大,难以实现.1967年,英国剑桥大学的Martin Richards对CP

《C专家编程》一1.11 轻松一下——由编译器定义的Pragmas效果

1.11 轻松一下--由编译器定义的Pragmas效果 自由软件基金会(Free Software Foundation)是一个独特的组织,它由MIT顶级黑客Richard Stallman所创立.顺便提一下,我们所说的"黑客",它的原先意思是"天才程序员".后来这个称呼被媒体所贬损,致使它在局外人眼中成了"邪恶的天才"的代名词.和形容词"bad"一样,"黑客"现在也有两个相反的意思,必须通过上下文才能明白

What's the best MATLAB equivalent? (open source or otherwise free)

What is the best open-source (or otherwise free) MATLAB equivalent, in your opinion, and why? Something with many built-in functions relevant to engineering and science, and a variety of good graphing capabilities. Syntax close to MATLAB's is a bonus

mfc-MFC与MATLAB混合编程生成可执行文件的移植性

问题描述 MFC与MATLAB混合编程生成可执行文件的移植性 我在matlab中建立一个函数并编译成dll,然后VS2008中的MFC调用dll,在我的电脑上成功运行,并生成可执行文件,也能成功运行.但是在别的电脑上就出现错误,错误原因是在我的电脑上DLL可以初始化,在别的电脑上初始化失败.求大神给看看!!!! 小弟没有C币,要不然一定悬赏100C币! 解决方案 需要带上Mfc库,Matlab库文件 解决方案二: 需要安装MCRInstaller 解决方案三: 参考下哦http://blog.c

tld-openTLD用vs(2010或2012或2013)+MATLAB(2011a或2013b)混合编程出现错误

问题描述 openTLD用vs(2010或2012或2013)+MATLAB(2011a或2013b)混合编程出现错误 error link 2019 ,网上说是链接器附加依赖项没设置好,但是ZK大神的源码用vs打开的属性页中根本就没有链接器的选项,我想知道error 2019是什么原因 和 源程序 vs2008工程打开后没有链接器这两个问题有哪位大神知道怎么解决啊,愁死了,配置了一个星期了,单独配置vs+opencv没有问题,MATLAB也能运行程序,就是混编有问题啊?还有OPENTLD纯C+

[iOS]日历和提醒编程指南(Calendar and Reminders Programming Guide)

[iOS]日历和提醒编程指南(Calendar and Reminders Programming Guide) 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 分析:事件提醒开发包(Event