Monday, April 27, 2009

Matlab: mex error: expected expression before '/' token

Few days ago I installed Matlab 2008b on CentOS 5.2, and I wanted to move my programs from Matlab 2007a on Mac X to the Matlab 2008b on CentOS 5.2. There were no problems with that, execpt my mex C files. When I wanted to compile my mex files in the new Matlab I was getting literally hundreds of errorsmy_c.c:653: error: expected expression before '/' token
my_c.c:655: error: expected expression before '/' token
my_c.c:686: error: expected expression before '/' token
my_c.c:688: error: expected expression before '/' token
my_c.c:688: error: expected expression before '/' token
The reason was that in my C files I was using "//" for doing comments, rather than "/* */". By default Matlab 2008b uses ansi standard (i.e. -ansi flag in gcc). The -ansi flag represents ISO C90 (equivalent to flag -std=c89) standard. In this standard there are no "//" for comments. Therefore, I could change all comments in my C files, or compile my C files using -std=c99 flag instead of -ansi flag. In c99 one can use "//" for comments. I choose the second option, and therefore I modified my mexopts.sh file.This file contains default options and flags to be used when executing mex command in matlab (i.e. compiling C files). In my case this this was in my home directory, i.e. ~/.matlab/R2008b/mexopts.sh. So I opened this file and edit parts for my computer. I had CentOS5.2 x64 bits, so I edited part #----------------------------------------------------------------------------
;;
glnxa64)
#----------------------------------------------------------------------------
Specifically, I changed CC='gcc'
CFLAGS=" -ansi -D_GNU_SOURCE"
toCC='gcc'
CFLAGS=' -std=c99'
CFLAGS=" $CFLAGS -D_GNU_SOURCE"
With this change I had no more errors regarding "//" comments.

Tip

To determine which options in mexopts.sh your mex compiler is using, one can just make an error or typo in some options and check if mex will crash or not.

1 comment:

  1. Anonymous8:17 AM

    thanks! replacing "//yadayada" with "/*yadayada*/" solved my problem!

    ReplyDelete