1

我正在尝试在 Visual C++ 中使用 MATLAB 中的 deploytool 生成的 C++ 共享库。我收到一个错误:无法正确初始化库。

我跟着:

http://www.mathworks.com/support/solutions/en/data/1-2QTWCE/http://blogs.mathworks.com/loren/2011/02/03/creating-c-shared-libraries-and -dlls/教程。

我的代码是:.cpp 文件是:

#include "stdafx.h"
#include "try2.h"
#include <iostream>
#include <string.h>
#include "mex.h"
#include "mclmcr.h"
#include "mclmcrrt.h"
#ifdef _DEBUG
#pragma comment(lib, "try2.lib")
#pragma comment(lib, "mclmcrrt.lib")
#pragma comment(lib, "mclcommain.lib")
#pragma comment(lib, "mclbase.lib")
#pragma comment(lib, "mclmcr.lib")
#pragma comment(lib, "mclxlmain.lib")
#else
#pragma comment(lib, "try2.lib")
#pragma comment(lib, "mclmcrrt.lib")
#pragma comment(lib, "mclcommain.lib")
#pragma comment(lib, "mclbase.lib")
#pragma comment(lib, "mclmcr.lib")
#pragma comment(lib, "mclxlmain.lib")
#endif
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
    /* Call the MCR and library initialization functions */

if( !mclInitializeApplication(NULL,0) )
{

      std::cerr << "Could not initialize the application properly."
                  << std::endl;
        return -1;
}
try2Initialize();
if (!try2Initialize())
{

     std::cerr << "Could not initialize the library properly."
                  << std::endl;
        return -1;
}


     mwArray img= "C:/Users/Desktop/dressimages/T1k5aHXjNqXXc4MOI3_050416.jpg";

            shoes(img);
            try2Terminate();
mclTerminateApplication();
            return 0;
}

从 MATLAB 生成的 try2.cpp 包装文件是

//
  // MATLAB Compiler: 4.18 (R2012b)
  // Date: Tue Apr 23 13:16:46 2013
  // Arguments: "-B" "macro_default" "-W" "cpplib:try2" "-T" "link:lib" "-d"
  // "C:\UsersDocuments\MATLAB\try2\src" "-N" "-p" "vision" "-p"
  // "stats" "-w" "enable:specified_file_mismatch" "-w" "enable:repeated_file"
  // "-w" "enable:switch_ignored" "-w" "enable:missing_lib_sentinel" "-w"
  // "enable:demo_license" "-v"
  // "C:\Users\Desktop\trial\shoes.m" "-a"
  // "C:\Users\Desktop\trial\color_space_file.mat" 
  //
#include <stdio.h>
#define EXPORTING_try2 1
#include "try2.h"
static HMCRINSTANCE _mcr_inst = NULL;
#if defined( _MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__) || defined(__LCC__)
#ifdef __LCC__
#undef EXTERN_C
#endif
#include <windows.h>
static char path_to_dll[_MAX_PATH];
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, void *pv)
{
    if (dwReason == DLL_PROCESS_ATTACH)
    {
        if (GetModuleFileName(hInstance, path_to_dll, _MAX_PATH) == 0)
            return FALSE;
    }
    else if (dwReason == DLL_PROCESS_DETACH)
    {
    }
    return TRUE;
}
#endif
#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_try2_C_API
#define LIB_try2_C_API /* No special import/export declaration */
#endif
LIB_try2_C_API 
bool MW_CALL_CONV try2InitializeWithHandlers(
    mclOutputHandlerFcn error_handler,
    mclOutputHandlerFcn print_handler)
{
    int bResult = 0;
  if (_mcr_inst != NULL)
    return true;
  if (!mclmcrInitialize())
    return false;
  if (!GetModuleFileName(GetModuleHandle("try2"), path_to_dll, _MAX_PATH))
    return false;
    {
        mclCtfStream ctfStream = 
            mclGetEmbeddedCtfStream(path_to_dll);
        if (ctfStream) {
            bResult = mclInitializeComponentInstanceEmbedded(   &_mcr_inst,
                                                                error_handler, 
                                                                print_handler,
                                                                ctfStream);
            mclDestroyStream(ctfStream);
        } else {
            bResult = 0;
        }
    }  
    if (!bResult)
    return false;
  return true;
}
LIB_try2_C_API 
bool MW_CALL_CONV try2Initialize(void)
{
  return try2InitializeWithHandlers(mclDefaultErrorHandler, mclDefaultPrintHandler);
}
LIB_try2_C_API 
void MW_CALL_CONV try2Terminate(void)
{
  if (_mcr_inst != NULL)
    mclTerminateInstance(&_mcr_inst);
}
LIB_try2_C_API 
void MW_CALL_CONV try2PrintStackTrace(void) 
{
  char** stackTrace;
  int stackDepth = mclGetStackTrace(&stackTrace);
  int i;
  for(i=0; i<stackDepth; i++)
  {
    mclWrite(2 /* stderr */, stackTrace[i], sizeof(char)*strlen(stackTrace[i]));
    mclWrite(2 /* stderr */, "\n", sizeof(char)*strlen("\n"));
  }
  mclFreeStackTrace(&stackTrace, stackDepth);
}
LIB_try2_C_API 
bool MW_CALL_CONV mlxShoes(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[])
{
  return mclFeval(_mcr_inst, "shoes", nlhs, plhs, nrhs, prhs);
}
LIB_try2_CPP_API 
void MW_CALL_CONV shoes(const mwArray& imagepath)
{
  mclcppMlfFeval(_mcr_inst, "shoes", 0, 0, 1, &imagepath);

  } 

当我尝试运行它时,它无法初始化 try2Initialize()。有什么解决办法?它是一个win32 控制台应用程序。

我正在使用 Visual Studio 2010、MATLAB R2012b-64bit,我的操作系统是 Windows 7。P>S。对不起,多余的代码只是想给出程序的细节。

4

1 回答 1

0

我不确定 R2012b 和 64 位版本。我正在使用 r2012a。但我想我可以分享我的故事。我一直使用 deploytool,所以生成的方法被命名为你的方法:try2Initialize(); 最近改成mcc命令,发现在c文件中,方法名还是一样的,但是如果我用一些3方工具检查dll文件中的方法名,比如dll export viewer:http://www. nirsoft.net/utils/dll_export_viewer.html

实际的方法名称是 _try2Initialize(); 前面有一点_

因此,如果您用尽选项,我建议您检查方法名称。祝你好运!

于 2014-11-28T10:33:03.117 回答