[Linux 64 bit] MEX seg fault on simple FORTRAN file
3 views (last 30 days)
Show older comments
Hi,
I've been following the documentation on mex routines and am trying to compile a simple timestwo.F example, located here: matlabroot/extern/examples/refbook/.
This gateway routine is listed here:
===================== begin timestwo.F =============================
#include "fintrf.h"
C
#if 0
C
C timestwo.F
C .F file needs to be preprocessed to generate .for equivalent
C
#endif
C
C timestwo.f
C
C Computational function that takes a scalar and doubles it.
C This is a MEX-file for MATLAB.
C Copyright 1984-2009 The MathWorks, Inc.
C $Revision: 1.12.2.7 $
subroutine mexFunction(nlhs, plhs, nrhs, prhs)
C-----------------------------------------------------------------------
C
mwpointer plhs(*), prhs(*)
mwpointer mxGetPr, mxCreateDoubleMatrix
mwpointer x_pr, y_pr
C-----------------------------------------------------------------------
C
integer nlhs, nrhs
integer mxIsNumeric
mwsize mxGetM, mxGetN
mwsize m, n, size
real*8 x, y
C Check for proper number of arguments.
if(nrhs .ne. 1) then
call mexErrMsgTxt('One input required.')
elseif(nlhs .gt. 1) then
call mexErrMsgTxt('Too many output arguments.')
endif
C Get the size of the input array.
m = mxGetM(prhs(1))
n = mxGetN(prhs(1))
size = m*n
C Check to insure the input is a number.
if(mxIsNumeric(prhs(1)) .eq. 0) then
call mexErrMsgTxt('Input must be a number.')
endif
C Create matrix for the return argument.
plhs(1) = mxCreateDoubleMatrix(m,n,0)
x_pr = mxGetPr(prhs(1))
y_pr = mxGetPr(plhs(1))
call mxCopyPtrToReal8(x_pr,x,size)
C Call the computational subroutine.
call timestwo(y, x)
C Load the data into y_pr, which is the output to MATLAB
call mxCopyReal8ToPtr(y,y_pr,size)
return
end
===================== end timestwo.F ===============================
here's the (verbose) output when I compile:
$ mex -fortran -v timestwo.F timestwo_sub.for
===================== begin compilation output ====================
-> mexopts.sh sourced from directory (DIR = .)
FILE = /mex/examples/mexopts.sh
----------------------------------------------------------------
-> MATLAB = /usr/local/bin/Matlab
-> CC = gcc-4.2
-> CC flags:
CFLAGS = -fPIC -fno-omit-frame-pointer -ansi -D_GNU_SOURCE -pthread -fexceptions
CDEBUGFLAGS = -g
COPTIMFLAGS = -O -DNDEBUG
CLIBS = -Wl,-rpath-link,/usr/local/bin/Matlab/bin/glnxa64 -L/usr/local/bin/Matlab/bin/glnxa64 -lmx -lmex -lmat -lm -lstdc++
arguments = -DMX_COMPAT_32
-> CXX = g++
-> CXX flags:
CXXFLAGS = -fPIC -fno-omit-frame-pointer -ansi -D_GNU_SOURCE -pthread
CXXDEBUGFLAGS = -g
CXXOPTIMFLAGS = -O -DNDEBUG
CXXLIBS = -Wl,-rpath-link,/usr/local/bin/Matlab/bin/glnxa64 -L/usr/local/bin/Matlab/bin/glnxa64 -lmx -lmex -lmat -lm
arguments = -DMX_COMPAT_32
-> FC = gfortran
-> FC flags:
FFLAGS = -fPIC -fno-omit-frame-pointer -fexceptions -fno-automatic
FDEBUGFLAGS = -g
FOPTIMFLAGS = -O
FLIBS = -Wl,-rpath-link,/usr/local/bin/Matlab/bin/glnxa64 -L/usr/local/bin/Matlab/bin/glnxa64 -lmx -lmex -lmat -lm
arguments = -DMX_COMPAT_32
-> LD = gfortran
-> Link flags:
LDFLAGS = -pthread -shared -Wl,--version-script,/usr/local/bin/Matlab/extern/lib/glnxa64/fexport.map -Wl,--no-undefined
LDDEBUGFLAGS = -g
LDOPTIMFLAGS = -O
LDEXTENSION = .mexa64
arguments =
-> LDCXX =
-> Link flags:
LDCXXFLAGS =
LDCXXDEBUGFLAGS =
LDCXXOPTIMFLAGS =
LDCXXEXTENSION =
arguments =
----------------------------------------------------------------
-> gfortran -c -I/usr/local/bin/Matlab/extern/include -I/usr/local/bin/Matlab/simulink/include -fPIC -fno-omit-frame-pointer -fexceptions -fno-automatic -DMX_COMPAT_32 -O "timestwo.F"
-> gfortran -c -I/usr/local/bin/Matlab/extern/include -I/usr/local/bin/Matlab/simulink/include -fPIC -fno-omit-frame-pointer -fexceptions -fno-automatic -DMX_COMPAT_32 -O "timestwo_sub.for"
-> gfortran -O -pthread -shared -Wl,--version-script,/usr/local/bin/Matlab/extern/lib/glnxa64/fexport.map -Wl,--no-undefined -o "timestwo.mexa64" timestwo.o timestwo_sub.o -Wl,-rpath-link,/usr/local/bin/Matlab/bin/glnxa64 -L/usr/local/bin/Matlab/bin/glnxa64 -lmx -lmex -lmat -lm
===================== end compilation output ======================
my compiler options were as follows:
FC='gfortran'
FFLAGS='-fPIC -fno-omit-frame-pointer -fexceptions -fno-automatic'
FLIBS="$RPATH $MLIBS -lm"
FOPTIMFLAGS='-O'
FDEBUGFLAGS='-g'
when I start up MATLAB and run the routine, it seg faults. This does not happen with the g95 compiler, and I can't figure out why this is the case. Does anyone have any suggestions?
Thanks,
Tim
0 Comments
Answers (1)
James Tursa
on 13 Feb 2012
The timestwo.f example only works for scalar inputs. How are you calling it? (Side Note: size is a Fortran intrinsic function name, so I would pick a different variable name in the code)
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!