2

I want to compile a C code in ubuntu using mex which is configured with gcc. I can smoothly compile the code in OSX. However, when I want to compile it in Linux, the compiler generates the error on the comment lines starting with // (it works fine with /* */. Since the program includes several header files from third-party libraries, I cannot substitute // with /* */. I would like to know whether there is any way around to overcome this problem.

MATLAB version: R2012b gcc version in Linux: 4.7.2 gcc version in OSX: 4.2.1

Any help is appreciated

Edit: Here is the command I use to compile the code:

mex -g -largeArrayDims -ldl TDSVDHNGateway.c

Here is the error generated by mex:

In file included from TDSVDHNGateway.c:2:0:
TDS.h:17:1: error: expected identifier or ‘(’ before ‘/’ token
TDS.h:26:2: error: unknown type name ‘index_t’
TDS.h:27:2: error: unknown type name ‘index_t’
In file included from TDSVDHNGateway.c:2:0:
TDS.h:146:3: error: unknown type name ‘index_t’
TDSVDHNGateway.c:37:3: error: unknown type name ‘index_t’
TDSVDHNGateway.c: In function ‘mexFunction’:
TDSVDHNGateway.c:166:25: error: ‘index_t’ undeclared (first use in this function)
TDSVDHNGateway.c:166:25: note: each undeclared identifier is reported only once for each function it appears in

Line 17 in header file is:

//Defining index_t
typedef size_t index_t;

If I replace //Defining index_t with /*Defining index_t*/ the code will be compiled with no problem.

Pouya
  • 1,871
  • 3
  • 20
  • 25
  • Can you list out the compiler errors? – Rhs Jul 31 '13 at 17:52
  • 2
    Please determine and post the exact command line which `mex` uses to invoke `gcc`. I suspect there is something in there which is disabling support for `//`-style comments (there are several options which do this (and other stuff as well)). – zwol Jul 31 '13 at 17:55
  • To allow C++ comments in C code call mex as follows: `mex CFLAGS="\$CFLAGS -Wp,-lang-c-c++-comments" mymexfile.c` – Praetorian Jul 31 '13 at 18:00

1 Answers1

10

From the gcc docs;

In GNU C, you may use C++ style comments, which start with ‘//’ and continue until the end of the line. Many other C implementations allow such comments, and they are included in the 1999 C standard. However, C++ style comments are not recognized if you specify an -std option specifying a version of ISO C before C99, or -ansi (equivalent to -std=c90).

Under Linux, by default mex adds -ansi, which disables C++ comments. Either update your mexopts file, replacing -ansi with -std=c99 or run mex with;

mex -g -largeArrayDims -ldl CFLAGS="\$CFLAGS -std=c99" TDSVDHNGateway.c
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294