Mex generates error for // while compiling C code

2020-04-12 09:44发布

问题:

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.

回答1:

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


标签: c gcc mex