Garima Singh
Garima Singh

Reputation: 1490

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

I am writing a mex code so that I can call C-functions from Matlab and I am using makefile.

My makefile returns errors when I try gcc compiler; but, compiles fine with g++. However, I would like gcc to work.

My Main code:

#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 that does not work:

  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 $^

If I just replace CXX = gcc by CXX = g++, it starts working.

Errors that I get:

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

Lines in main code, where errors occur, are marked by string // ERROR HERE.

Any help would be appreciated.

Upvotes: 0

Views: 2207

Answers (1)

Klas Lindb&#228;ck
Klas Lindb&#228;ck

Reputation: 33273

double& is a reference to a double in C++. C doesn't have references, only pointers, hence the error.

If you want gcc to treat your file as c++ code, rename it so it has a c++ extension (for example .cpp).

Upvotes: 2

Related Questions