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

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!

Answers (2)

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 Comment

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?

Sign in to comment.

Anyone can help, please?

8 Comments

The code you posted does not agree with the line numbers of the error messages. Either you trimmed some comments in posting, or else the line numbers are after expansion of the include and the invalid preprocessor directive is inside the file included. Or both.
Thanks, I trimmed some comments in posting, and now it is corrected. Do you have any idea on solving the problem, please?
When I look at https://gcc.gnu.org/onlinedocs/gfortran/Preprocessing-Options.html I notice that ".f" is not on the list of extensions for which -cpp is automatically enabled. I see ".F" on the list, but the list is case-sensitive since it specifically lists .fpp as distinct from .FPP
Thanks. I changed the file into .F. But it still errors:
>> mex -setup fortran
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.
**************************************************************************
>> cd ~/WORK/RZT/matlabtest/
>> mex calculateG.F
/usr/bin/ld: cannot find -lgfortranbegin
collect2: ld returned 1 exit status
mex: link of ' "calculateG.mexa64"' failed.
Error using mex (line 206)
Unable to complete successfully.
>>
I find suggestion that it might be called libgfortranbegin . Or it might just need a -L mex option to point it to the right directory.
I also find reference to
apt-get install gfortran-multilib
I have already installed GNU gcc4.4.7, do I still need to install GNU gfortran 4.3.x ? I am using Matlab R2013a on Linux 64.
https://cn.mathworks.com/content/dam/mathworks/mathworks-dot-com/support/sysreq/files/SystemRequirements-Release2013a_SupportedCompilers.pdf
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

Sign in to comment.

Categories

Asked:

on 28 Feb 2017

Commented:

on 14 Mar 2017

Community Treasure Hunt

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

Start Hunting!