• 大小: 60KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-07
  • 语言: C/C++
  • 标签: c++与matlab  

资源简介

这个例子演示了通过mcc将.m函数编译成动态链接库供c++调用的方式实现c++和matlab的交互。具体例子介绍请结合我的博文:blog.csdn.net/arackethis/article/details/43372553

资源截图

代码片段和文件信息

//
// MATLAB Compiler: 4.17 (R2012a)
// Date: Sun Feb  1 08:33:19 2015
// Arguments: “-B“ “macro_default“ “-W“ “cpplib:libmyFunc“ “-T“ “link:lib“
// “myFunc.m“ “-c“ 
//

#include 
#define EXPORTING_libmyFunc 1
#include “libmyFunc.h“

static HMCRINSTANCE _mcr_inst = NULL;


#ifdef __cplusplus
extern “C“ {
#endif

static int mclDefaultPrintHandler(const char *s)
{
  return mclWrite(1 /* stdout */ s sizeof(char)*strlen(s));
}

#ifdef __cplusplus
} /* End extern “C“ block */
#endif

#ifdef __cplusplus
extern “C“ {
#endif

static int mclDefaultErrorHandler(const char *s)
{
  int written = 0;
  size_t len = 0;
  len = strlen(s);
  written = mclWrite(2 /* stderr */ s sizeof(char)*len);
  if (len > 0 && s[ len-1 ] != ‘\n‘)
    written += mclWrite(2 /* stderr */ “\n“ sizeof(char));
  return written;
}

#ifdef __cplusplus
} /* End extern “C“ block */
#endif

/* This symbol is defined in shared libraries. Define it here
 * (to nothing) in case this isn‘t a shared library. 
 */
#ifndef LIB_libmyFunc_C_API
#define LIB_libmyFunc_C_API /* No special import/export declaration */
#endif

LIB_libmyFunc_C_API 
bool MW_CALL_CONV libmyFuncInitializeWithHandlers(
    mclOutputHandlerFcn error_handler
    mclOutputHandlerFcn print_handler)
{
    int bResult = 0;
  if (_mcr_inst != NULL)
    return true;
  if (!mclmcrInitialize())
    return false;
    {
        mclCtfStream ctfStream = 
            mclGetembeddedCtfStream((void *)(libmyFuncInitializeWithHandlers));
        if (ctfStream) {
            bResult = mclInitializeComponentInstanceembedded(   &_mcr_inst
                                                                error_handler 
                                                                print_handler
                                                                ctfStream);
            mclDestroyStream(ctfStream);
        } else {
            bResult = 0;
        }
    }  
    if (!bResult)
    return false;
  return true;
}

LIB_libmyFunc_C_API 
bool MW_CALL_CONV libmyFuncInitialize(void)
{
  return libmyFuncInitializeWithHandlers(mclDefaultErrorHandler mclDefaultPrintHandler);
}

LIB_libmyFunc_C_API 
void MW_CALL_CONV libmyFuncTerminate(void)
{
  if (_mcr_inst != NULL)
    mclTerminateInstance(&_mcr_inst);
}

LIB_libmyFunc_C_API 
void MW_CALL_CONV libmyFuncPrintStackTrace(void) 
{
  char** stackTrace;
  int stackDepth = mclGetStackTrace(&stackTrace);
  int i;
  for(i=0; i  {
    mclWrite(2 /* stderr */ stackTrace[i] sizeof(char)*strlen(stackTrace[i]));
    mclWrite(2 /* stderr */ “\n“ sizeof(char)*strlen(“\n“));
  }
  mclFreeStackTrace(&stackTrace stackDepth);
}


LIB_libmyFunc_C_API 
bool MW_CALL_CONV mlxMyFunc(int nlhs mxArray *plhs[] int nrhs mxArray *prhs[])
{
  return mclFeval(_mcr_inst “myFunc“ nlhs plhs nrhs prhs);
}

LIB_libmyFunc_CPP_API 
void MW_CALL_CONV myFunc(int nargout mwArray& C const mwArray& A const mwArray& B)
{
  mclcppMlfFeval(_mcr_inst “myFunc“ nargout 1 2 &C &A &B);

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件       20956  2015-02-01 00:33  matlabEngine_example1\libmyFunc.ctf
     文件          46  2015-01-31 20:39  matlabEngine_example1\myFunc.m
     文件      356533  2015-02-01 00:33  matlabEngine_example1\mccExcludedFiles.log
     文件        2659  2015-02-01 00:33  matlabEngine_example1\libmyFunc.h
     文件        3003  2015-02-01 00:33  matlabEngine_example1\libmyFunc.cpp
     文件        1265  2015-01-31 23:00  matlabEngine_example1\main.cpp
     文件         114  2015-02-01 00:33  matlabEngine_example1\libmyFunc.exports
     文件        2146  2015-02-01 00:33  matlabEngine_example1\readme.txt
     文件       20509  2015-02-01 03:04  matlabEngine_example1\main
     文件       45372  2015-01-31 21:22  matlabEngine_example1\libmyFunc.so
     目录           0  2015-02-01 03:04  matlabEngine_example1\

评论

共有 条评论

相关资源