Thread Subject: mex files and & operator

Subject: mex files and & operator

From: Petros Mpogiatzis

Date: 23 Nov, 2009 12:42:03

Message: 1 of 2

Is there some problem with "&" operator and mex compiler? It try to compile for example the following (test.c):


----------------------------------------------------------------------------------------------------
#include "mex.h"

void add_them(double &k,double x, double y)
{
k=x+y;
}

void mexFunction(
    int nlhs, mxArray *plhs[],
    int nrhs, const mxArray *prhs[])
{
  double x,y,k;
     
  // get the inputs
  x=mxGetScalar(prhs[0]);
  y=mxGetScalar(prhs[1]);
    
  
  // call the function
  add_them(k,x,y);

  //create the output
  plhs[0]=mxCreateDoubleScalar(k);
  
  return;
}
------------------------------------------------------------------------------------------------------

and I get:


>>mex test.c
Error test.c: 3 syntax error; found `&' expecting `)'
Error test.c: 3 skipping `&' `k' `,'
Error test.c: 3 missing name for parameter 1 to function `add_them'
Error test.c: 3 syntax error; found `double' expecting `{'
Error test.c: 3 missing identifier
Error test.c: 3 syntax error; found `double' expecting `;'
Error test.c: 3 syntax error; found `)' expecting `;'
Error test.c: 3 skipping `)' `{'
Error test.c: 5 undeclared identifier `k'
Warning test.c: 3 possible usage of y before definition
Warning test.c: 3 possible usage of x before definition
Error test.c: 20 too many arguments to `add_them'
Error test.c: 20 too many arguments to `add_them'
Warning test.c: 12 possible usage of k before definition
11 errors, 3 warnings
------------------------------------------------------------------------------------------------------------------------


Thank you for your time

Subject: mex files and & operator

From: Rune Allnor

Date: 23 Nov, 2009 13:07:00

Message: 2 of 2

On 23 Nov, 13:42, "Petros Mpogiatzis" <pain...@geo.auth.gr> wrote:
> Is there some problem with "&" operator and mex compiler?

Depends on what compiler and language you use.

> It try to compile for example the following (test.c):

The .c ending indicates to the compiler that you
want to compile C code, as opposed to C++ or fortran,
or whatever other languages are supported through MEX.
It is your responsibility as programmer to ensure that

1) What you write is indeed valid C code.
2) You use a compiler that supports the language
   features you use in the code. C is a constantly
   evolving language, and not all compilers support
   all features.

> void add_them(double &k,double x, double y)

Don't know what the C99 standard says about this,
but this is not valid pre-C99 C. It is, however,
valid C++. But you already indicated to the compiler
through the .c ending of your file name that you
write C, not C++.

Rune

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
mex Petros Mpogiatzis 23 Nov, 2009 07:44:08
c Petros Mpogiatzis 23 Nov, 2009 07:44:08
syntax error fo... Petros Mpogiatzis 23 Nov, 2009 07:44:08
rssFeed for this Thread

Contact us at files@mathworks.com