lonstud
lonstud

Reputation: 525

matlab crashes without dump file when using fopen for file

I am using gnumex with mingw for compiling mex files in matlab in Windows OS. I am not able to use fopen command to open files. Following is the code which I am using.

#include <stdio.h>
#include "mex.h"
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
    FILE  *fp=NULL;
    fp = fopen("test.txt", "w+");
}

The program compiles succesfully but when I try to run the compile mexw64 file, matlab simply closes down without generating any dump file.

Note that I am able to run simple Hello world program and passing and receiving arguments using other mex programs.

Update1: adding flcose(fp) does not change behaviour.

Update2: I cannot debug using http://www.mathworks.com/help/matlab/matlab_external/debugging-on-microsoft-windows-platforms.html as I am compiling using mingw along with gnumex setup in windows

Update 3: Output of

mex -v file.cpp

 This is mex, Copyright 1984-2007 The MathWorks, Inc. 


-> Default options filename found in C:\Users\achaudhary\AppData\Roaming\MathWorks\MATLAB\R2009b 
---------------------------------------------------------------- 
->    Options file           = C:\Users\achaudhary\AppData\Roaming\MathWorks\MATLAB\R2009b\mexopts.bat 
      MATLAB                 = C:\PROGRA~1\MATLAB\R2009b 
->    COMPILER               = gcc 
->    Compiler flags: 
         COMPFLAGS           = -c -DMATLAB_MEX_FILE -x c++ 
         OPTIMFLAGS          = -O3 
         DEBUGFLAGS          = -g 
         arguments           =  
         Name switch         = -o 
->    Pre-linking commands   =  
->    LINKER                 = C:\PROGRA~1\MATLAB\R2009b\sys\perl\win32\bin\perl.exe E:\MATLAB~1\linkmex.pl 
->    Link directives: 
         LINKFLAGS           =   -mwindows -LC:\Users\ACHAUD~1\AppData\Roaming\MATHWO~1\MATLAB\R2009b\gnumex GM_ISCPP   -mwindows 
         LINKDEBUGFLAGS      = -g  -Wl,--image-base,0x28000000\n 
         LINKFLAGSPOST       =  
         Name directive      = -o file.mexw64 
         File link directive =  
         Lib. link directive =  
         Rsp file indicator  =  
->    Resource Compiler      = C:\PROGRA~1\MATLAB\R2009b\sys\perl\win32\bin\perl.exe E:\MATLAB~1\rccompile.pl --unix -o mexversion.res 
->    Resource Linker        =  
---------------------------------------------------------------- 


--> gcc  -c -DMATLAB_MEX_FILE -x c++ -oC:\USERS\ACHAUD~1\APPDATA\LOCAL\TEMP\MEX_FZ~1\file.obj -IC:\PROGRA~1\MATLAB\R2009b\extern\include -IC:\PROGRA~1\MATLAB\R2009b\simulink\include -O3 -DMX_COMPAT_32 file.cpp 


--> C:\PROGRA~1\MATLAB\R2009b\sys\perl\win32\bin\perl.exe E:\MATLAB~1\linkmex.pl -o file.mexw64   -mwindows -LC:\Users\ACHAUD~1\AppData\Roaming\MATHWO~1\MATLAB\R2009b\gnumex GM_ISCPP   -mwindows -s  C:\USERS\ACHAUD~1\APPDATA\LOCAL\TEMP\MEX_FZ~1\file.obj   

link command: g++ -shared C:\Users\ACHAUD~1\AppData\Roaming\MATHWO~1\MATLAB\R2009b\gnumex\mex.def -o file.mexw64 -mwindows -LC:\Users\ACHAUD~1\AppData\Roaming\MATHWO~1\MATLAB\R2009b\gnumex  -mwindows -s C:\USERS\ACHAUD~1\APPDATA\LOCAL\TEMP\MEX_FZ~1\file.obj -llibmx -llibmex -llibmat 

EDIT I am able to work with minGw now but the problem still persists with cygwin. The solution to working with cygwin still not found.

Upvotes: 2

Views: 368

Answers (1)

lonstud
lonstud

Reputation: 525

Looked at the windgb debugger and found out that mex file was using libraries from cygwin64 from earlier installation as well. To make the previous installation work, I had to remove -mno-cygwin flag from the mexopts.bat generated by gnumex manually as it was incompatible with the installed gcc version. Somehow, everything apart from FILE open worked.

So did a fresh install of mingw64 and appended environment paths accordingly. Then reinstalled gnumex using mingw64 and now everything works fine with minGw.

Note: The problem still remains when working with cygwin.

Upvotes: 1

Related Questions