我正在使用gnumex和mingw編譯Windows操作系統中matlab中的mex文件。我無法使用fopen命令打開文件。以下是我正在使用的代碼。當文件使用fopen時,matlab崩潰時沒有dump文件
#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+");
}
程序成功地編譯,但是當我嘗試運行編譯mexw64文件,MATLAB乾脆倒閉,而不會產生任何轉儲文件。
請注意,我能夠運行簡單的Hello World程序並使用其他mex程序傳遞和接收參數。
更新1:添加flcose(fp)不會改變行爲。
UPDATE2:我不能調試使用http://www.mathworks.com/help/matlab/matlab_external/debugging-on-microsoft-windows-platforms.html,因爲我使用的MinGW一起在Windows
更新3 gnumex設置編譯:
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
編輯 我能使用MinGW工作的輸出現在,但問題仍然存在與cygwin。使用cygwin的解決方案仍未找到。
否'fclose'?我認爲你在這裏的帖子已經過於簡化了。一定要使用NULL終止的字符串。 – chappjc 2015-02-12 06:47:28
你試過調試你的mex文件嗎? http://www.mathworks.com/help/matlab/matlab_external/debugging-on-microsoft-windows-platforms.html – Shai 2015-02-12 07:18:44
@chappjc這是mex文件中的完整代碼。沒有過於簡單。請讓我在我需要的地方確保使用NULL終止的字符串。 – lonstud 2015-02-12 10:12:29