Mex在编译Linux中的C代码时会为//产生错误 [英] Mex generates error for // while compiling C code in Linux

查看:255
本文介绍了Mex在编译Linux中的C代码时会为//产生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ubuntu中使用配置了gcc的 mex 来编译C代码。我可以顺利编译OSX中的代码。但是,当我想在Linux中编译它时,编译器会在以 // 开头的注释行上生成错误(它可以很好地与 / * * / 。由于该程序包含来自第三方库的几个头文件,所以我不能用 / *替换 // * /
我想知道是否有任何方法可以解决这个问题。

MATLAB版本:R2012b
在Linux中的gcc版本:4.7.2
在OSX中的gcc版本:4.2.1



任何帮助表示赞赏



编辑:
下面是我用来编译代码的命令:

  mex  - g -largeArrayDims -ldl TDSVDHNGateway.c 

这是mex产生的错误:

 在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'
从TDSVDHNGateway.c包含的文件中:2:0:
TDS.h:146:3:错误:未知类型名称'index_t'
TDSVDHNGateway.c:37:3 :错误:未知类型名称'index_t'
TDSVDHNGateway.c:在函数'mexFunction'中:
TDSVDHNGateway.c:166:25:错误:'index_t'未声明(首次在此函数中使用)
TDSVDHNGateway.c:166:25:注意:每个未声明的标识符仅对于出现在


$ b $中的每个函数报告一次b

头文件中的第17行是:

  //定义index_t 
typedef size_t index_t;

如果我将 //定义index_t 替换为 / *定义index_t * / 代码将被编译时没有问题。

解决方案

来自 gcc文档;


在GNU C中,您可以使用C ++样式的注释,以'//'开头并继续直到行尾。许多其他C实现允许这样的评论,并且它们被包括在1999 C标准中。但是,如果您在C99之前指定了指定ISO C版本的-std选项,或者-ansi(等同于-std = c90),则无法识别C ++样式注释。


在Linux下,默认 mex 添加 -ansi ,它禁用C ++注释。或者更新你的mexopts文件,用 -std = c99 替换 -ansi 或者使用;

运行mex b
$ b

  mex -g -largeArrayDims -ldl CFLAGS =\ $ CFLAGS -std = c99TDSVDHNGateway.c 


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.

解决方案

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

这篇关于Mex在编译Linux中的C代码时会为//产生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆