【问题标题】:mex file: compilation fails with with "//" comment; but compiles fine when used "/* */" [duplicate]mex 文件:编译失败并带有“//”注释;但是在使用“/* */”时编译得很好[重复]
【发布时间】:2015-01-27 16:35:13
【问题描述】:

我正在编写一个将 C 代码链接到 matlab 的 mex 文件。

这是我的简单 mex 文件,它什么也不做,编译得很好。

#include "mex.h"

#ifndef N
#define N 100
#endif

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

}

但是如果我改变评论,像这样:

#include "mex.h"

#ifndef N
#define N 100
#endif

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

}

然后我收到以下错误:

>> 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 */

     ^

另外,如果我将其中一个文件保存为 C++ 文件,那么无论我使用 // 还是 /* .... */.

有人能告诉我为什么“//”不能用于评论吗?

【问题讨论】:

  • 也在herehere 中回答。 (是的,自我推销,但关键是有很多答案可以帮助解决这个问题)。

标签: c matlab mex


【解决方案1】:

有回复here

特别是,

在 Linux 下,默认情况下,mex 添加 -ansi,它会禁用 C++ cmets。

【讨论】:

  • 如果您发现重复,请留下评论链接,以便问题可以转化为重定向,而不是添加重复的答案。
  • @Chistakovitch 感谢您重定向到原始问题。以下命令工作正常: mex -g -largeArrayDims -ldl CFLAGS="\$CFLAGS -std=c99" mexcallingmatlab.c 你能解释一下“-largeArrayDims”在这里做什么吗?
  • @GarimaSingh 这与你无关。这是问题作者使用的论点。你唯一感兴趣的部分是CFLAGS="\$CFLAGS -std=c99"。 :)
【解决方案2】:

可能 Matlab 使用不启用 C99 功能的 gcc 参数调用 C 编译器。由于 C++ 风格的 cmets 在 C99 之前不是 C 标准的一部分—— gcc 会发出错误。

您可以在运行mex 时显式设置CFLAGS 以添加-std=c99。这应该使您能够使用 C++ 样式的 cmets(和其他功能)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 2021-10-07
    • 1970-01-01
    • 2015-08-21
    相关资源
    最近更新 更多