1

我想要做的是将 MATLAB-Compiler dll/lib 集成到一个新的 c++ 项目中。

我按照以下说明进行操作:如何集成从 MATLAB 生成的 C++ 共享库,该库运行良好(没有构建错误,并且 intelisense 运行良好,因此它会接缝所有必需的信息)。

我正在使用一个非常简单的 mathlab 代码/函数进行测试:

function output = extest( arg1,arg2 )
    output = arg1+arg2;
end

以及 matlab 函数的“默认”c++ 代码:

#include "extest.h"
#include <cstdlib>
#include <stdio.h>

int main(int argc, char** argv){

    mclmcrInitialize();
    if (!mclInitializeApplication(NULL,0)){

        std::cerr << "could not initialize the application properly" << std::endl;
        return -1;
    }
        if(!extestInitialize()){
            std::cerr << "could not initialize the library properly" << std::endl;
            return -1;
        }
        else{
            try{

                //code itself (not jet reached therefore removed)

            }catch(const mwException& e){
              std::cerr << e.what() << std::endl;
              return -2;
            }
            catch(...){
              std::cerr << "Unexpected error thrown" << std::endl;
              return -3;  
            }
            extestTerminate();
        }
        mclTerminateApplication();
        return 0;
}

在调试器尝试运行该行之后的 e 片刻之后,if(!extestInitialize())将引发以下错误。

在 DllTestingCpp.exe 中的 0x000002BF72E0EE55 处引发异常:0xC0000005:访问冲突读取位置 0x0000000000000008。

在此处输入图像描述

我可以点击 Visual Studioscontinue >按钮,并在点击 20 次后继续。通过(无需调试)启动代码ctrl + F5一切正常。

任何想法为什么会在调试模式下发生?或者更好的方法是如何摆脱这个错误?

PS:extest是我的库名称,使用 Matlab R2017a 64 位和 Visual Studio 2017(使用 x64 调试),

4

2 回答 2

1

我有同样的问题(使用 VS2019),我在这里找到了以下答案: https ://uk.mathworks.com/matlabcentral/answers/182851-how-do-i-integrate-my-c-shared-library-generated -来自-matlab-r2013b-in-visual-studio-2013

我遇到了同样的问题并将其报告给 Mathworks。他们回应说,对于 VS2013 及更高版本,调试器设置为在 0xc0000005 发生时中断,即使在这种情况下它是由 JVM 处理的。修复方法是转到“调试”>“Windows”>“异常设置”>“Win32”并取消选中“0xc0000005 访问冲突”。在 VS2012 中,此设置默认未选中。

这似乎工作正常

于 2019-07-17T16:08:35.673 回答
1

对我来说同样的问题(Matlab2017 + VS 2015)。可能与 MATLAB 使用的 java 存在一些冲突。

我已经修复了

const char *args[] = {"-nojvm"};
const int count = sizeof(args) / sizeof(args[0]);
mclInitializeApplication(args, count))

代替

mclInitializeApplication(NULL,0)
于 2018-01-25T10:12:56.837 回答