Matlab engine in Fortran code (gfortran problem)
5 次查看(过去 30 天)
显示 更早的评论
Hi,
I followed the instructions http://www.mathworks.nl/help/techdoc/matlab_external/f38569.html to interface some matlab models in my fortran code. When compiling the project with Intel Fortran compiler I use:
-fpp -I/usr/local/MATLAB/R2011b/extern/include -I/usr/local/MATLAB/R2011b/simulink/include -fexceptions -DMX_COMPAT_32
for compiling and:
-Wl,-rpath-link,/usr/local/MATLAB/R2011b/bin/glnxa64 -L/usr/local/MATLAB/R2011b/bin/glnxa64 -leng -lmx -lm
for linking and everything works fine!
When I compile with gfortran, I use the same commands (except -fpp which becomes -cpp) and I get a segmentation fault on the last line of:
mwPointer ep, mVx0
double precision Vx0
Vx0=1.d0
ep = engOpen('matlab ')
if (ep .eq. 0) then
write(log,*) 'Can''t start MATLAB engine'
stop
endif
mVx0 = mxCreateDoubleMatrix(1, 1, 0)
call mxCopyReal8ToPtr(Vx0, mxGetPr(mVx0), 1)
Anyone can help out?
Petros
0 个评论
回答(1 个)
James Tursa
2012-4-27
In Fortran you should never pass literal constant arguments to the API functions since they can vary in size among different platforms. You can get away with this in C/C++ because arguments are passed by value and are automatically converted to the correct type, but you can't get away with this practice in Fortran. Instead of doing this:
mVx0 = mxCreateDoubleMatrix(1, 1, 0)
call mxCopyReal8ToPtr(Vx0, mxGetPr(mVx0), 1)
you should be doing this:
mwPointer, external :: mxCreateDoubleMatrix, mxGetPr
mwSize :: one = 1
integer*4 :: complexity = 0
:
mVx0 = mxCreateDoubleMatrix( one, one, complexity )
call mxCopyReal8ToPtr( Vx0, mxGetPr(mVx0), one )
That way the integer arguments should always be the correct type regardless of platform.
You might also be interested in this Fortran 95 interface package:
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Fortran with MATLAB 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!