0

I am writing a mex file linking C code to matlab.

Here is my simple mex file which does not do anything and compiles fine.

#include "mex.h"

#ifndef N
#define N 100
#endif

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    /*validate input/output arguments */

}

But if I change the comment, like this:

#include "mex.h"

#ifndef N
#define N 100
#endif

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    //validate input/output arguments

}

then I get the following error:

>> mex mexcallingmatlab.c
Building with 'gcc'.
Warning: You are using gcc version '4.8.2'. The version of gcc is not supported. The version currently supported with MEX is '4.7.x'. For a list of currently supported
compilers see: http://www.mathworks.com/support/compilers/current_release. 
Warning: You are using gcc version '4.8.2-19ubuntu1)'. The version of gcc is not supported. The version currently supported with MEX is '4.7.x'. For a list of currently
supported compilers see: http://www.mathworks.com/support/compilers/current_release. 
Error using mex
/home/dkumar/Mex_Codes_DKU/Mex_C_Codes_DKU2/mexcallingmatlab.c: In function ‘mexFunction’:
/home/dkumar/Mex_Codes_DKU/Mex_C_Codes_DKU2/mexcallingmatlab.c:9:5: error: expected expression before ‘/’ token
     // validate input/output arguments */

     ^

Also, if I save either of the file as C++ file, then it always compiles whether I use // or /* .... */.

Could someone please tell me why "//" is not working for commenting?

Garima Singh
  • 1,410
  • 5
  • 22
  • 46
  • Also answered in [here](http://stackoverflow.com/a/22236710/2778484) and [here](http://stackoverflow.com/a/22236567/2778484). (yeah, self promotion, but the point is that there are more than a few answers to to help solve this). – chappjc Jan 29 '15 at 02:23

2 Answers2

1

There is a response here.

In particular,

Under Linux, by default mex adds -ansi, which disables C++ comments.

Community
  • 1
  • 1
Chostakovitch
  • 965
  • 1
  • 9
  • 33
  • 1
    If you find a duplicate, please leave a comment linking to it so the question can be turned into a redirect, instead of adding duplicated answers. – Ben Voigt Jan 27 '15 at 16:41
  • @Chostakovitch thanks for redirecting to original question. Following commands works fine: mex -g -largeArrayDims -ldl CFLAGS="\$CFLAGS -std=c99" mexcallingmatlab.c Could you please explain what "-largeArrayDims" does here? – Garima Singh Jan 27 '15 at 16:44
  • 2
    @GarimaSingh This is nothing related to you. This is an argument that the author of the question used. The only interesting part for you is `CFLAGS="\$CFLAGS -std=c99"`. :) – Chostakovitch Jan 27 '15 at 16:46
1

Probably Matlab calls the C compiler with gcc arguments that do not enable C99 features. Since C++ style comments were not part of the C standard before C99 -- gcc emits an error.

You can explicitly set CFLAGS when running mex to add -std=c99. This should enable you to use C++ style comments (and other features).

nimrodm
  • 23,081
  • 7
  • 58
  • 59