简体   繁体   English

在64位Windows 7上使用mingw-w64在C / C ++中通过engOpen调用MATLAB时崩溃

[英]Crash when calling MATLAB via engOpen in C/C++ with mingw-w64 on 64 bit Windows 7

I am trying to figure out why my C/C++ program crashes when I call MATLAB's engine with engOpen() . 我试图弄清楚为什么我用engOpen()调用MATLAB的引擎时C / C ++程序崩溃。 The code below prints Hello World and then throws an APPCRASH . 下面的代码将打印Hello World ,然后抛出APPCRASH

What might be causing this crash, or how do I debug from here? 是什么导致此崩溃,或者如何从此处调试?

testMATLAB.c testMATLAB.c

#include <stdio.h>
#include "engine.h"

int main(){
printf("Hello World.\n");
    Engine *ep;
        if (!(ep = engOpen(NULL))) {
            printf("Can't start MATLAB engine\n");
        } else {
            printf("Opening Matlab Engine\n");
        }

return 0;
}

输出“ Hello World”后,可执行文件在运行时崩溃。

More Details 更多细节

I am using: 我在用:

  • MATLAB R2012a MATLAB R2012a
  • mingw-w64 gcc version 4.8.0 mingw-w64 gcc版本4.8.0
  • 64 bit Windows 7 64位Windows 7

In response to a number of existing SO Q&As, I have 针对一些现有的SO问答,我有

  • confirmed that I am linking to C:\\Program Files\\MATLAB\\R2012a\\extern\\lib\\win64\\microsoft\\libeng.lib and C:\\Program Files\\MATLAB\\R2012a\\extern\\lib\\win64\\microsoft\\libmx.lib . 确认我正在链接到C:\\Program Files\\MATLAB\\R2012a\\extern\\lib\\win64\\microsoft\\libeng.libC:\\Program Files\\MATLAB\\R2012a\\extern\\lib\\win64\\microsoft\\libmx.lib
  • included C:\\Program Files\\MATLAB\\R2012a\\bin\\win64 in my path which contains libengine.dll and libmx.dll 我的路径中包含libengine.dlllibmx.dll C:\\Program Files\\MATLAB\\R2012a\\bin\\win64 libmx.dll
  • ran matlab /regserver from the windows command prompt 从Windows命令提示符下运行matlab /regserver

Note, if I place the engOpen() inside an if statement that isn't executed, then the code runs to completion. 请注意,如果我将engOpen()放置在未执行的if语句中,则代码将运行至完成状态。

I also do not think this is inherent to mingw because on a 32 bit WinXP machine I had no problem using MATLAB 2011 with mingw32 . 我也不认为这是mingw固有的,因为在32位WinXP机器上,将MATLAB 2011与mingw32一起使用时我没有问题。

Compiler and linker output is at http://pastebin.com/z9CAsb6X 编译器和链接器的输出位于http://pastebin.com/z9CAsb6X

How did you link the *.lib? 您如何链接* .lib? On my project I use PostgreSQL and instead of linking the provided MSVC-compiled *.lib I had to convert it to *.a. 在我的项目中,我使用PostgreSQL,而不是链接提供的MSVC编译的* .lib,而是将其转换为* .a。 I think it was a procedure like this one: 我认为这是一个这样的程序:

http://oldwiki.mingw.org/index.php/CreateImportLibraries http://oldwiki.mingw.org/index.php/CreateImportLibraries

I link using those DLL directly rather than lib files, say: libeng.dll, libut.dll, libmex.dll and libmat.dll . 我直接使用这些DLL而不是lib文件进行链接,例如: libeng.dll, libut.dll, libmex.dll and libmat.dll

My setup is MinGW 4.8.1 64bit, Win7 64bit, Matlab 64bit . 我的设置是MinGW 4.8.1 64bit, Win7 64bit, Matlab 64bit

It works for me. 这个对我有用。

I had a similar problem using Visual Studio. 使用Visual Studio时遇到类似的问题。 I think it has to do with the way Matlab 64 bit handles warnings after it closes. 我认为这与Matlab 64位关闭后处理警告的方式有关。 I had to add: (Mathworks solved my problem there) 我必须添加:(Mathworks在这里解决了我的问题)

warning('off','MATLAB:ClassInstanceExists') % Add this line

to my matlab code, which probably meant there was some matlab class that was not really needed in my code, but still matlab compiler wanted to find? 对我的matlab代码而言,这可能意味着我的代码中确实并不需要某些matlab类,但是matlab编译器仍然想找到吗? (my guess...) (我猜...)

If this does not work directly you might want to disable more warnings?... 如果这直接不起作用,您可能要禁用更多警告?...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM