vs c++ 调用matlab编译的dll

来源:互联网 发布:markdown c语言高亮 编辑:程序博客网 时间:2024/05/08 01:53
C++ 调用 Matlab 生成的DLL (Visual Studio 2008)
2011-08-17 23:06

又折腾一整天, 在linux 里折腾过一次了,换成visual studio 同样要折腾,更烦和怪。

2012-02-16

 又折腾一整天. 错误总在重复..不做完备记录是很不好的习惯!!

 这次是重新写了一个库, 换了名字, 废弃以前的库.   然后在VS中的库 依赖设置总是搞不定, 总是去链接那个被我废弃的库!!

 

 

 

-------------------------------

 

1.  using namespace  boost

  这句话一定要放到所有 #include之后, 否则,例如

  using namespace boost;

  #include ”mcr.h “

 那么mcr.h也会使用boost中的名字, 很容易就变量定义冲突了。    

 

2.   用Matlab 编译生成库文件   

mcc -W cpplib:dlltest -T link:lib test.m

结果,我们将会得到包含dlltest .dll, dlltest .h,dlltest .lib等在内的一些文件。接下来我们只需要这三个文件。

此时,打开dlltest.h看看,在文件的最下面我们可以发现C++接口的函数定义。仔细观察过后,我们可以发现,这个接口函数的参数是按照这样的顺序定义的:输出参数的个数、输出参数、以及输入参数。

 

3.  Visual Studio的配置 

   然后在VC6中创建一个VC++工程(我创建了个基于对话框做测试),取名为VCMATLAB.把以上四个文件拷贝到VC++工程的源代码所在目录。

 1)工程中加入头文件:dlltest .h

2)设置工程的引用库:project—》links里添加mclmcrrt.lib dlltest.lib(注:debug和release是相互独立的)

   具体的, 在 link -> Input -> 附加依赖项 ,添加: 

   "D:\Program Files\MATLAB\R2009a\extern\lib\win32\microsoft\mclmcrrt.lib"
   "F:\Kara's Doc\Research\CODE,Program\C++\LeastCost\ncor_cost.lib"

   注意: 要用引号,否则 回报 ” fatal error LNK1104: cannot open file 'C:\Program.obj'“的错误, 不知道为什么。

 

3) 设置 库的路径. 工具--> 选项 --> 项目和解决方案--> VC++ 目录, 设置lib的头文件, 以及lib文件的目录.

   另外注意, dlltest .dll 一定要放到当前工程的目录下( 我也不知道为什么, 但目前的结果是这样,否则报dll丢失错误. 

   即便这个dll 文件在库的目录下. )

 



 

 3. vc工程加入代码如下

  Matlab 中定义的函数: function [cost,x, exist_zero] = NCOR_COST (piJ, D_J, m ) , 

      在dlltest.h 文件中,有函数声明 :

    extern void   NCOR_COST(int nargout  // 输出参数个数
                                                         , mwArray& cost  //输出1
                                                         , mwArray& x              // 输出2
                                                         , mwArray& exist_zero  // 输出 3
                                                         , const mwArray& piJ    // 输入1
                                                         , const mwArray& D_J     // 输入2
                                                         , const mwArray& m);      // 输入3

 

#include "mclmcr.h" // mwArray声明

#include "dlltest.h"

void CVCMATLABDlg::OnButton1() 
{
// TODO: Add your control notification handler code here

  if( ! mclInitializeApplication(NULL,0) )

       {        fprintf(stderr, "Could not initialize the application.\n");

              exit(1);

       }

// 初始化库

       if (! dlltestInitialize() )

       {     fprintf(stderr,"Could not initialize the library.\n");

              exit(1);

       }



mwArray x(10,10,mxDOUBLE_CLASS);
double *xData;
xData = new double[100];
int i;
for( i=0; i<100; ++i){
xData[i] = -5+(0.1*i);
}
x.SetData(xData, 100);
mwArray y(10,10,mxDOUBLE_CLASS);
huatu_test(1,y,x);  
dlltestTerminate();                              
mclTerminateApplication(); 

}

 

3. mwArray 

  mwArray :C++用它向MATLAB传递输 i/o 参数。MATLAB中的参数都是矩阵表示,哪怕是1*1的矩阵。

 1)构造函数

  mwArray(mwSize num_dims, const mwSize* dims, mxClassID mxID,mxComplexity cmplx = mxREAL)

  num_rows :行数; num_cols :列数;mxID:The data type type of the matrix.: cmplx:The complexity of the matrix (numeric types only).

   mwArray a(2, 2, mxDOUBLE_CLASS);

   mwArray b(3, 3, mxSINGLE_CLASS, mxCOMPLEX);

   mwArray c(2, 3, mxCELL_CLASS); 

    mwArray(mwSize num_rows, mwSize num_cols, mxClassID mxID,mxComplexity cmplx = mxREAL)

     mwArray(const char* str)

   mwArray(mwSize num_strings, const char** str)

mwArray(mwSize num_rows, mwSize num_cols, int num_fields,const char** fieldnames)

mwArray(mwSize num_dims, const mwSize* dims, int num_fields,const char** fieldnames)

mwArray(const mwArray& arr)

mwArray(<type> re)

mwArray(<type> re, <type> im)

2) 方法

 

mwArray SharedCopy() const

mwArray Serialize() const

mxClassID ClassID() const

int ElementSize() const

size_t ElementSize() const

mwSize NumberOfElements() const

mwSize NumberOfNonZeros() const

mwSize MaximumNonZeros() const

mwSize NumberOfDimensions() const

int NumberOfFields() const

mwString GetFieldName(int index)

mwArray GetDimensions() const

bool IsEmpty() const

bool IsSparse() const

bool IsNumeric() const

bool IsComplex() const

bool Equals(const mwArray& arr) const

int CompareTo(const mwArray& arr) const

int HashCode() const

mwString ToString() const

mwArray RowIndex() const

mwArray ColumnIndex() const

void MakeComplex()

mwArray Get(mwSize num_indices, ...)

mwArray Get(const char* name, mwSize num_indices, ...)

mwArray Get(mwSize num_indices, const mwIndex* index)

mwArray Get(const char* name, mwSize num_indices, const mwIndex*index)

      #include "mclcppclass.h"

      double data[4] = {1.0, 2.0, 3.0, 4.0};

      double x;

       mwArray a(2, 2, mxDOUBLE_CLASS); 

      a.SetData(data, 4);

      x = a.Get(1,1); // x = 1.0

       x = a.Get(2, 1, 2); // x = 3.0

       x = a.Get(2, 2, 2); // x = 4.0

 


mwArray Real()

mwArray Imag()

void Set(const mwArray& arr)

void GetData(<numeric-type>* buffer, mwSize len) const

void GetLogicalData(mxLogical* buffer, mwSize len) const

void GetCharData(mxChar* buffer, mwSize len) const

void SetData(<numeric-type>* buffer, mwSize len)

     #include "mclcppclass.h"

     double rdata[4] = {1.0, 2.0, 3.0, 4.0};

     double data_copy[4] ;

     mwArray a(2, 2, mxDOUBLE_CLASS);

     a.SetData(rdata, 4);

     a.GetData(data_copy, 4);  // 取出a中的数据。

void SetLogicalData(mxLogical* buffer, mwSize len)

void SetCharData(mxChar* buffer, mwSize len)

原创粉丝点击