Win7 32bit + Matlab2013b +Visual Studio 2010聯合編程配置


 

要建立獨立運行的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 的Command Window 下輸入命令 ,配置生成器

 

>> 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/R2011b/win32.html
 
Please choose your compiler for building standalone MATLAB applications:
 
Would you like mbuild to locate installed compilers [y]/n? y

(注:若是第一次mbuild -setup,選n,再在給出的的選項中選擇2010 compiler,要注意安裝路徑是否相符)

 

Select a compiler:
[1] Microsoft Software Development Kit (SDK) 7.1
[2] Microsoft Visual C++ 6.0
[3] Microsoft Visual C++ 2008 SP1
[4] Microsoft Visual C++ 2010
[5] Microsoft Visual C++ 2012
 
[0] None
 
Compiler: 4
 

 

 

Your machine has a Microsoft Visual C++ 2010 compiler located at
E:\Program Files\VS2010. Do you want to use this compiler [y]/n? y
 
Please verify your choices:
 
Compiler: Microsoft Visual C++ 2010  
Location: E:\Program Files\VS2010
 
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\Yanlu\AppData\Roaming\MathWorks\MATLAB\R2013a\compopts.bat
From template:              E:\PROGRA~1\MATLAB~1\bin\win32\mbuildopts\msvc100compp.bat
 
Done . . .

 

 

然后輸入命令

>> mex – setup

 以類似的方法配置編譯器。Matlab 的配置完成.

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/R2011b/win32.html
 
Please choose your compiler for building MEX-files:
 
Would you like mex to locate installed compilers [y]/n? y
 
Select a compiler:
[1] Lcc-win32 C 2.4.1 in D:\PROGRA~1\MATLAB\R2011b\sys\lcc
[2] Microsoft Visual C++ 2010 in D:\Program Files\Microsoft Visual Studio 10.0
[3] Microsoft Visual C++ 2008 SP1 in E:\Program Files\Microsoft Visual Studio 9.0
 
[0] None
 
Compiler: 2

 

 
Please verify your choices:
 
Compiler: Microsoft Visual C++ 2010  
Location: E:\Program Files\VS2010
 
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\Yanlu\AppData\Roaming\MathWorks\MATLAB\R2013a\mexopts.bat
From template:              E:\PROGRA~1\MATLAB~1\bin\win32\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/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html  
           Building with the -largeArrayDims option enables the new API.
**************************************************************************

 

 

 

2.轉化BoostImage.m文件

 mcc -W cpplib:BoostImage -T link:lib BoostImage -d F:\zhangying\xieyulin\MatlabToC\

注:在此的m文件function最好對應end,否則容易出現 Error using mcc的錯誤。(但我碰到有個m文件仍不能轉化成功,不知什么原因)

其中,mccMatlab提供的編譯命令,對於這一點,Matlab的幫助說明如下:
-W lib:string link:lib其中-W是控制編譯之后的封裝格式,cpplib,是指編譯成C++libcpplib冒號后面是指編譯的庫的名字,

-T表示目標,link:lib表示要連接到一個庫文件的目標,目標的名字是后面的BoostImage,即你寫的m函數的名字。
運行完之后,你的目錄下面,將會出現以下一些文件:

BoostImage.cpp
BoostImage.exp
BoostImage.dll
BoostImage.exports
BoostImage.h
BoostImage.lib
mccExcludedFiles.log
readme.txt

具體每一個文件的意思,請查幫助,這里只說其中很有用的幾個:
BoostImage.dll是編譯好的動態庫,BoostImage這個函數封裝在里面,BoostImage.h是對應的頭文件,在C里面調用這個函數的時候,需要include這個頭文件在這個頭文件的最后,你可以看到下面的內容:
extern LIB_BoostImage_CPP_API void MW_CALL_CONV BoostImage(int nargout, mwArray& output_im, const mwArray& input_im, const mwArray& Mboost);

這就是BoostImageC函數的聲明。nargout表示輸出變量的個數,abc就是對應了m函數的幾個變量。
注意,abc三個變量的類型是mwArray,這是Matlab提供的一個特殊的變量類型(在7.0的版本,編譯之后,變量類型是mxArray)。mwArray是一個類,具體可以查幫助。

2.系統環境設置

 

我的電腦->屬性->高級->環境變量->用戶變量->PATH中添加路徑:D:\Program Files\MATLAB\R2011b\bin\win32,重啟生效。

 

3.VS2010環境配置

新建一個win32 控制台應用程序,命名為MatlabTest.

右鍵MatlabTest解決方案->properties->VC++ Derections

 (1)在“Executable Directions”項目欄,添加:D:\Program Files\MATLAB\R2011b\bin\win32(注意:當前的Mathlab安裝在D:\Program Files下面)
(2)在“include Directions”項目欄,添加:D:\Program Files\MATLAB\R2011b\extern\include
(3)在“Library Directions”項目欄,添加:D:\Program Files\MATLAB\R2011b\extern\lib\win32\microsoft
上述環境只需要配置一次。

另參考:

http://blog.csdn.net/stardust94605/article/details/8972064

在此暫時先測試了VS2010調用matlab現有函數的功能:

//  MatlabTest.cpp : Defines the entry point for the console application.
//

#include  " stdafx.h "
#include <stdlib.h>   
#include <stdio.h>   
#include < string.h>   
#include  " engine.h "   
#include  " matrix.h "   
#pragma comment(lib,"libeng.lib")    
#pragma comment(lib,"libmx.lib")   
int main()  
{  
    Engine *ep;  
     int i , j ;  
     // show how to open MATLAB engine   
    
// for remote ones:   
    
// engOpen( ADDRESS OF REMOTE SYSTEM ) ;   
     if (!(ep = engOpen( " \0 "))){  
        fprintf(stderr,  " \nCan't start MATLAB engine\n ");  
         return EXIT_FAILURE;  
    }  
     // show how to create matrix   
    mxArray *Y = mxCreateDoubleMatrix( 1 ,  3 , mxREAL) ;  
      
     // show how to put data in matrix   
     double tmp[ 3] = { 1.0 ,  2.0 ,  3.0} ;  
    memcpy(mxGetPr(Y) , tmp ,  sizeof(tmp)) ;  
     // show how to put variables in the Engine   
    engPutVariable(ep ,  " Y " , Y) ;  
     // show how to execute commands in MATLAB   
    engEvalString(ep,  " X = ones(5,1) * Y ");  
      
     // show how to get variables from the Engine   
    mxArray *X = engGetVariable(ep ,  " X ") ;  
      
     // show how to manipulate dimensions   
     int dims[ 10] ;  
     int ndims ;  
    ndims = mxGetNumberOfDimensions(X) ;  
    printf( " total number of dimensions is %d\n " , ndims) ;  
    memcpy(dims , mxGetDimensions(X) , ndims *  sizeof( int)) ;  
     for ( i =  0 ; i < ndims ; i ++ ){  
        printf( " dimension %d : %d\n " , i , dims[i]) ;  
    }  
    printf( " \n ") ;  
     // show how the data is stored in the memory   
     double *p = ( double*)mxGetData(X) ;      
     for ( i =  0 ; i < dims[ 0] ; i ++ ){  
         for ( j =  0 ; j < dims[ 1] ; j ++ ){  
            printf( " %8.2f " , p[j * dims[ 0] + i]) ;  
        }  
        printf( " \n ") ;  
    }  
     // ---important, to release resources   
    mxDestroyArray(X) ;  
    mxDestroyArray(Y) ;  
     // show how to hide and unhide MATLAB command window   
    printf( " type RETURN to hide the MATLAB command window...\n ") ;  
    getchar() ;  
    engSetVisible(ep ,  false) ;  
    printf( " type RETURN to unhide the MATLAB command window...\n ") ;  
    getchar() ;  
    engSetVisible(ep ,  true) ;  
    printf( " type RETURN to END this program...\n ") ;  
    getchar() ;      
     // remembering to close it is important .   
    
// but if you are debugging your programs ,    
    
// annotate the following line will save you a lot of time ,   
    
// for you needn't to restart the Engine .   
    engClose(ep) ;  
      
     // when your work is accomplished , type "exit" in MATLAB command window   
     return EXIT_SUCCESS;  
}

 


 

 

 

運行結果:


 

若出現如下問題:

 error C2371: 'char16_t' : redefinition; different basic types   

在混合編程中,也許會出現如下錯誤:

 —————————————————————————————————————————————————

C:Microsoft Visual Studio 10.0VCINCLUDEyvals.h(576) : error C2371: 'char16_t' : redefinition; different basic types
C:Matlabr2010aexternincludematrix.h(330) : see declaration of 'char16_t'

 —————————————————————————————————————————————————

原因是VS2010中的yvals.h添加了char16_t的定義,而Matlab的matrix.h也包含對char16_t的定義,所以同時包含這兩個頭文件的話,會導致重復定義char16_t的錯誤。

解決方案:

參考:http://maciejgryka.com/bits/2011/09/char16_t-conflict-while-mexing-with-vs2010/
完全沒有必要修改這兩個頭文件(以防修改之后,會在將來導致一些問題),只需要在包含matrix.h之前包含yvals.h即可。例如:

#include <yvals.h>
#if (_MSC_VER >= 1600)
#define __STDC_UTF_16__
#endif
#include "mex.h"

注:mex.h 包含matrix.h。這就要求我們必須知道所包含的頭文件是否包含matrix.h。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM