Mex fortran code on Matlab R2013a, Linux 64: Illegal preprocessor directive

6 次查看(过去 30 天)
Hello,
I am using Matlab R2013a on Linux 64 platform, trying to mex fortran code. The gcc version is gcc-4.4.7.
[zhumin@ln1 ~]$ echo $PATH
/apps/lib/gcc-4.4.7-install/bin:/apps/lib/gcc-4.7.1/bin:/apps/intel/impi/4.0.2.003/intel64/bin:/apps/intel/Compiler/11.1/069/f/bin/intel64:/apps/intel/Compiler/11.1/069/c/bin/intel64:/apps/intel/Compiler/11.1/069/f/bin/intel64:/apps/intel/Compiler/11.1/069/c/bin/intel64:/apps/intel/impi/4.0.2.003/intel64/bin:/jobmgr/lsf8.0/8.0/linux2.6-glibc2.3-x86_64/etc:/jobmgr/lsf8.0/8.0/linux2.6-glibc2.3-x86_64/bin:/usr/lib64/qt-3.3/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/zhumin/bin
[zhumin@ln1 ~]$ echo $LD_LIBRARY_PATH
/apps/lib/gcc-4.4.7-install/lib64:/apps/lib/gcc-4.8.4/gcc-4.8.4/contrib/mpfr-2.4.2/lib:/apps/lib/gcc-4.8.4/gcc-4.8.4/contrib/gmp-4.3.2/lib:/apps/lib/gcc-4.8.4/gcc-4.8.4/contrib/mpc-0.8.1/lib:/apps/lib/gcc-4.7.1/lib64:/apps/intel/impi/4.0.2.003/intel64/lib:/apps/intel/Compiler/11.1/069/f/lib/intel64:/apps/intel/Compiler/11.1/069/f/mkl/lib/em64t:/apps/intel/Compiler/11.1/069/c/lib/intel64:/apps/intel/Compiler/11.1/069/c/ipp/em64t/sharedlib:/apps/intel/Compiler/11.1/069/c/mkl/lib/em64t:/apps/intel/Compiler/11.1/069/c/tbb/intel64/cc4.1.0_libc2.4_kernel2.6.16.21/lib:/usr/lib64:/usr/lib:/apps/intel/Compiler/11.1/069/f/lib/intel64:/apps/intel/Compiler/11.1/069/f/mkl/lib/em64t:/apps/intel/Compiler/11.1/069/c/lib/intel64:/apps/intel/Compiler/11.1/069/c/ipp/em64t/sharedlib:/apps/intel/Compiler/11.1/069/c/mkl/lib/em64t:/apps/intel/Compiler/11.1/069/c/tbb/intel64/cc4.1.0_libc2.4_kernel2.6.16.21/lib:/apps/intel/impi/4.0.2.003/intel64/lib:/jobmgr/lsf8.0/8.0/linux2.6-glibc2.3-x86_64/lib
Then when I mex the fortran file calculateG.f in Matlab, it errors "Illegal preprocessor directive".
>> mex -setup
Options files control which compiler to use, the compiler and link command
options, and the runtime libraries to link against.
Using the 'mex -setup' command selects an options file that is
placed in /home/zhumin/.matlab/R2013a and used by default for 'mex'. An options
file in the current working directory or specified on the command line
overrides the default options file in /home/zhumin/.matlab/R2013a.
To override the default options file, use the 'mex -f' command
(see 'mex -help' for more information).
The options files available for mex are:
1: /apps/soft/MATLAB/R2013a/bin/mexopts.sh :
Template Options file for building MEX-files
0: Exit with no changes
Enter the number of the compiler (0-1):
1
Overwrite /home/zhumin/.matlab/R2013a/mexopts.sh ([y]/n)?
y
/apps/soft/MATLAB/R2013a/bin/mexopts.sh is being copied to
/home/zhumin/.matlab/R2013a/mexopts.sh
**************************************************************************
Warning: The MATLAB C and Fortran API has changed to support MATLAB
variables with more than 2^32-1 elements. In the near future
you will be required to update your code to utilize the new
API. You can find more information about this at:
http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html
Building with the -largeArrayDims option enables the new API.
**************************************************************************
>> mex calculateG.f
Warning: calculateG.f:1: Illegal preprocessor directive
calculateG.f:10.6:
mwPointer plhs(*), prhs(*)
1
Error: Unclassifiable statement at (1)
......
How to solve this problem, please? Thanks!

回答(2 个)

James Tursa
James Tursa 2017-2-28
The warning indicates that something went wrong with the pre-processor. The error indicates that mwPointer is not defined. What is supposed to happen is you include this file at the front of your Fortran source code:
#include "fintrf.h"
And then that file contains the pre-processor directives that get mwPointer defined. For some reason, this didn't happen properly. Can you try mexing with the -v option to maybe get more information on the problems that are occurring?
  1 个评论
Ren Zetian
Ren Zetian 2017-3-1
编辑:Ren Zetian 2017-3-13
Thank you. I tried to mex with -v, it showed that:
>> mex -v calculateG.f
**************************************************************************
Warning: Neither -compatibleArrayDims nor -largeArrayDims is selected.
Using -compatibleArrayDims. In the future, MATLAB will require
the use of -largeArrayDims and remove the -compatibleArrayDims
option. For more information, see:
http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html
**************************************************************************
-> mexopts.sh sourced from directory (DIR = $PREF_DIR)
FILE = /home/zhumin/.matlab/R2013a/mexopts.sh
----------------------------------------------------------------
-> MATLAB = /apps/soft/MATLAB/R2013a
-> CC = gcc
-> CC flags:
CFLAGS = -ansi -D_GNU_SOURCE -fexceptions -fPIC -fno-omit-frame-pointer -pthread
CDEBUGFLAGS = -g
COPTIMFLAGS = -O -DNDEBUG
CLIBS = -Wl,-rpath-link,/apps/soft/MATLAB/R2013a/bin/glnxa64 -L/apps/soft/MATLAB/R2013a/bin/glnxa64 -lmx -lmex -lmat -lm -lstdc++
arguments = -DMX_COMPAT_32
-> CXX = g++
-> CXX flags:
CXXFLAGS = -ansi -D_GNU_SOURCE -fPIC -fno-omit-frame-pointer -pthread
CXXDEBUGFLAGS = -g
CXXOPTIMFLAGS = -O -DNDEBUG
CXXLIBS = -Wl,-rpath-link,/apps/soft/MATLAB/R2013a/bin/glnxa64 -L/apps/soft/MATLAB/R2013a/bin/glnxa64 -lmx -lmex -lmat -lm
arguments = -DMX_COMPAT_32
-> FC = gfortran
-> FC flags:
FFLAGS = -fexceptions -fbackslash -fPIC -fno-omit-frame-pointer
FDEBUGFLAGS = -g
FOPTIMFLAGS = -O
FLIBS = -Wl,-rpath-link,/apps/soft/MATLAB/R2013a/bin/glnxa64 -L/apps/soft/MATLAB/R2013a/bin/glnxa64 -lmx -lmex -lmat -lm
arguments = -DMX_COMPAT_32
-> LD = gfortran
-> Link flags:
LDFLAGS = -pthread -shared -Wl,--version-script,/apps/soft/MATLAB/R2013a/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/apps/soft/MATLAB/R2013a/extern/include -I/apps/soft/MATLAB/R2013a/simulink/include -fexceptions -fbackslash -fPIC -fno-omit-frame-pointer -DMX_COMPAT_32 -O "calculateG.f"
Warning: calculateG.f:1: Illegal preprocessor directive
calculateG.f:10.6:
mwPointer plhs(*), prhs(*)
1
Error: Unclassifiable statement at (1)
calculateG.f:12.6:
mwPointer mxCreateDoubleMatrix, mxGetPr, mxGetM, mxGetN
1
Error: Unclassifiable statement at (1)
calculateG.f:13.6:
mwPointer m1, n1, m2, n2, m3, n3, m4, n4, m5, n5
1
Error: Unclassifiable statement at (1)
calculateG.f:14.6:
mwPointer MatX_pr, MatY_pr, MatU_pr, MatV_pr
1
Error: Unclassifiable statement at (1)
calculateG.f:15.6:
mwPointer MatGin_pr, MatGcount_pr, MatGout_pr
1
Error: Unclassifiable statement at (1)
calculateG.f:30.6:
plhs(1) = mxCreateDoubleMatrix(m5,n5,0)
1
Error: Unclassifiable statement at (1)
calculateG.f:7.38:
Subroutine mexFunction(nlhs,plhs,nrhs,prhs)
1
Error: Symbol 'plhs' at (1) has no IMPLICIT type
....
And my fortran file begins with:
#include "fintrf.h"
!==========
!========== Interface Routine =======
!==========
!==========
!==========
Subroutine mexFunction(nlhs,plhs,nrhs,prhs)
Implicit None
!==========
mwPointer plhs(*), prhs(*)
Integer nlhs, nrhs
mwPointer mxCreateDoubleMatrix, mxGetPr, mxGetM, mxGetN
mwPointer m1, n1, m2, n2, m3, n3, m4, n4, m5, n5
mwPointer MatX_pr, MatY_pr, MatU_pr, MatV_pr
mwPointer MatGin_pr, MatGcount_pr, MatGout_pr
Real*8 MatX(50), MatY(310), MatU(15000), MatV(15000)
Real*8 MatGin(15000), MatGcount, MatGout(15000)
!==========
m1 = mxGetM(prhs(1))
n1 = mxGetN(prhs(1))
m2 = mxGetM(prhs(2))
n2 = mxGetN(prhs(2))
How to fix this, please?

请先登录,再进行评论。


Ren Zetian
Ren Zetian 2017-3-10
Anyone can help, please?
  8 个评论
Ren Zetian
Ren Zetian 2017-3-14
https://cn.mathworks.com/content/dam/mathworks/mathworks-dot-com/support/sysreq/files/SystemRequirements-Release2013a_SupportedCompilers.pdf
Walter Roberson
Walter Roberson 2017-3-14
Possibly you need to install gfortran to get the library, but I am not sure. You appear to have passed the compile stage; the error now is in the link stage. You should locate either lgfortran or libgfortran and add -L to point to the right directory and -l to point to the right library name

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by