MATLAB mex on Linux: Using C++ style comments in C mex files

December 2nd, 2011 | Categories: Making MATLAB faster, matlab, programming | Tags:

I recently spent a lot of time optimizing a MATLAB user’s code and made extensive use of mex files written in C.  Now, one of the habits I have gotten into is to mix C and C++ style comments in my C source code like so:

/* This is a C style comment */

// This is a C++ style comment

For this particular project I did all of the development on Windows 7 using MATLAB 2011b and Visual Studio 2008 which had no problem with my mixing of comments. Move over to Linux, however, and it’s a very different story. When I try to compile my mex file

mex block1.c -largeArrayDims 

I get the error message

block1.c:48: error: expected expression before ‘/’ token

The fix is to call mex as follows:

mex block1.c -largeArrayDims CFLAGS="\$CFLAGS -std=c99"

Hope this helps someone out there. For the record, I was using gcc and MATLAB 2011a on 64bit Scientific Linux.

  1. December 9th, 2011 at 17:29
    Reply | Quote | #1

    That’s a very helpful point, but I’ll add something there. You can edit your compiler options if you enter your home directory, then find the fiile .matlab/MATLAB_VERSION/ and edit it. In there you’ll find the compiler options for each architecture. Just edit the options for the architecture you’re using. Particularly, I find useful that I can change the compiler options and add my favourite optimisation parameters. :)

  2. David K
    February 17th, 2012 at 20:37
    Reply | Quote | #2

    Very Helpful.

  3. al.
    April 23rd, 2013 at 17:55
    Reply | Quote | #3

    Just what I needed! Thanks! :-)