【问题标题】:Mex generates error for // while compiling C code in Linux在 Linux 中编译 C 代码时,Mex 为 // 生成错误
【发布时间】:2013-07-31 17:51:19
【问题描述】:

我想在 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 产生的错误:

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

头文件第17行是:

//Defining index_t
typedef size_t index_t;

如果我将//Defining index_t 替换为/*Defining index_t*/,代码将毫无问题地编译。

【问题讨论】:

  • 你能列出编译器错误吗?
  • 请确定并发布mex 用来调用gcc 的确切命令行。我怀疑那里有一些东西正在禁用对//-style cmets 的支持(有几个选项可以做到这一点(以及其他东西))。
  • 允许 C 代码中的 C++ cmets 调用 mex 如下:mex CFLAGS="\$CFLAGS -Wp,-lang-c-c++-comments" mymexfile.c

标签: c gcc mex


【解决方案1】:

来自the gcc docs

在 GNU C 中,您可以使用 C++ 样式的 cmets,它以“//”开头并一直持续到行尾。许多其他 C 实现允许这样的 cmets,它们包含在 1999 C 标准中。 但是,如果您指定 -std 选项来指定 C99 之前的 ISO C 版本或 -ansi(等效于 -std=c90),则无法识别 C++ 样式的 cmets。

在 Linux 下,默认情况下 mex 添加 -ansi,这会禁用 C++ cmets。更新您的 mexopts 文件,将 -ansi 替换为 -std=c99 或运行 mex;

mex -g -largeArrayDims -ldl CFLAGS="\$CFLAGS -std=c99" TDSVDHNGateway.c

【讨论】:

    猜你喜欢
    • 2017-01-27
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-02
    • 1970-01-01
    • 2017-01-30
    相关资源
    最近更新 更多