gcc compiler and makefile: error: expected identifier or ‘(’ before ‘&’ token

gcc compiler and makefile: error: expected identifier or ‘(’ before ‘&’ token

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

当我尝试 gcc 编译器时,我的 makefile returns 错误;但是,用 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)。