c - gcc 编译器和 makefile : error: expected identifier or ‘(’ before ‘&’ token

标签 c matlab makefile mex

我正在编写一个 mex 代码,以便我可以从 Matlab 调用 C 函数,并且我正在使用 makefile。

当我尝试使用 gcc 编译器时,我的 makefile 返回错误;但是,用 g++ 编译得很好。但是,我希望 gcc 能够工作。

我的主要代码:

#include "mex.h"
#include <math.h>

#include "Include_4_TSNNLS.h"
#define pi (3.141592653589793)

extern void _main();

const int numInputArgs  = 3;
const int numOutputArgs = 1;

// Function declarations.
// -----------------------------------------------------------------
double  getMatlabScalar    (const mxArray* ptr);
double& createMatlabScalar (mxArray*& ptr);     // ERROR HERE

// Function definitions.
// -----------------------------------------------------------------
void mexFunction (int nlhs, mxArray *plhs[],
          int nrhs, const mxArray *prhs[]) {


  int res = TestingLibraries() ; 

  // Check to see if we have the correct number of input and output
  // arguments.
  if (nrhs != numInputArgs)
    mexErrMsgTxt(" DKU-1: Incorrect number of input arguments");
  if (nlhs != numOutputArgs)
    mexErrMsgTxt("DKU-2: Incorrect number of output arguments");

  // Get the inputs.
  double x  = getMatlabScalar(prhs[0]);
  double mu = getMatlabScalar(prhs[1]);
  double v  = getMatlabScalar(prhs[2]);

  // Create the output. It is also a double-precision scalar.
  double& p = createMatlabScalar(plhs[0]);    ;     // ERROR HERE

  // Compute the value of the univariate Normal at x.
  p = exp(-(x-mu)*(x-mu)/(2*v)) / sqrt(2*pi*v);     ;     // ERROR HERE
}

double getMatlabScalar (const mxArray* ptr) {

  // Make sure the input argument is a scalar in double-precision.
  if (!mxIsDouble(ptr) || mxGetNumberOfElements(ptr) != 1)
    mexErrMsgTxt("The input argument must be a double-precision scalar");

  return *mxGetPr(ptr);
}

double& createMatlabScalar (mxArray*& ptr) {   ;     // ERROR HERE
  ptr = mxCreateDoubleMatrix(1,1,mxREAL);
  return *mxGetPr(ptr);
}

无法运行的 Makefile:

  MEXSUFFIX  = mexa64
  MATLABHOME = /usr/local/MATLAB/R2014b
  MEX        = mex
  CXX        = gcc

  CFLAGS     = -fPIC -pthread -DMX_COMPAT_32 \
               -DMATLAB_MEX_FILE


  LIBS      = -lm
  INCLUDE   = -I$(MATLABHOME)/extern/include
  MEXFLAGS  = -cxx CC='$(CXX)' CXX='$(CXX)' LD='$(CXX)'

  REBUILDABLES = *.o *.mexa64     ## Anything with these extension

  TARGET_WO_EXTN =  normpdfDKU
  TARGET =  $(TARGET_WO_EXTN).$(MEXSUFFIX)

  all : $(TARGET)
    echo All done

  clean : 
    rm -f $(REBUILDABLES)   
    echo Clean done

  $(TARGET): Include_4_TSNNLS.o $(TARGET_WO_EXTN).o
    $(MEX) $(MEXFLAGS) $(LIBS) -output $(TARGET_WO_EXTN) $^

  $(TARGET_WO_EXTN).o: $(TARGET_WO_EXTN).c
    $(CXX) $(CFLAGS) $(INCLUDE) -c $^

  Include_4_TSNNLS.o:  Include_4_TSNNLS.c
    $(CXX) $(CFLAGS) $(INCLUDE) -c $^

如果我只是将 CXX = gcc 替换为 CXX = g++,它就会开始工作。

我得到的错误:

dkumar@dkumar-Precision-WorkStation-T7500 ~/Mex_Codes_DKU/Using_tsnnls_DKU_copy_MEX $ make
gcc -fPIC -pthread -DMX_COMPAT_32 -DMATLAB_MEX_FILE -I/usr/local/MATLAB/R2014b/extern/include -c Include_4_TSNNLS.c
gcc -fPIC -pthread -DMX_COMPAT_32 -DMATLAB_MEX_FILE -I/usr/local/MATLAB/R2014b/extern/include -c normpdfDKU.c
normpdfDKU.c:18:7: error: expected identifier or ‘(’ before ‘&’ token
 double& createMatlabScalar (mxArray*& ptr);
       ^
normpdfDKU.c: In function ‘mexFunction’:
normpdfDKU.c:41:9: error: expected identifier or ‘(’ before ‘&’ token
   double& p = createMatlabScalar(plhs[0]);
         ^
normpdfDKU.c:44:3: error: ‘p’ undeclared (first use in this function)
   p = exp(-(x-mu)*(x-mu)/(2*v)) / sqrt(2*pi*v);
   ^
normpdfDKU.c:44:3: note: each undeclared identifier is reported only once for each function it appears in
normpdfDKU.c: At top level:
normpdfDKU.c:56:7: error: expected identifier or ‘(’ before ‘&’ token
 double& createMatlabScalar (mxArray*& ptr) { 
       ^
make: *** [normpdfDKU.o] Error 1

主代码中发生错误的行由字符串 //ERROR HERE 标记。

如有任何帮助,我们将不胜感激。

最佳答案

double& 是对 C++ 中的 double 的引用。 C 没有引用,只有指针,因此出错。

如果您希望 gcc 将您的文件视为 c++ 代码,请重命名它,使其具有 c++ 扩展名(例如 .cpp)。

关于c - gcc 编译器和 makefile : error: expected identifier or ‘(’ before ‘&’ token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28564862/

相关文章:

C 修改数组的意外行为

C:在不知道其类型的情况下复制数组

c - 将二维数组写入C中的文件

c - 解析C语言中以逗号分隔的字符串 - Arduino

matlab - 用 block 状或聚合的方法随机替换矩阵中的元素

matlab - 如何绘制与其虚部相关的复杂系统

matlab - 在 MATLAB 中为依赖属性动态分配 getter

c - 从不同目录引用头文件时出错

linux - 从哪里获取 iostream.h

通过 Makefile 的命令行参数