[Create mexfunction from C code] warning: implicit declaration of function

50 views (last 30 days)
why I got this warning message "warning: implicit declaration of function 'myfunction' " when I build the mex function?
#include <mex.h>
#include <matrix.h>
#include<stdio.h>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
int argc = 0;
char **argv;
int i, result;
argc = nrhs;
argv = (char **) mxCalloc( argc, sizeof(char *) );
for (i = 0; i < nrhs; i++){
if( !mxIsChar( prhs[i] ) ){
mexErrMsgTxt("Input must be of type char.");
return;
}
argv[i] = mxArrayToString( prhs[i] );
}
result = myfunction( argc, argv );
for( i=argc-1; i<=0; i-- )
mxFree( argv[i] );
mxFree( argv );
if( result )
mexErrMsgTxt("main function causes an error");
}
float square ( float x )
{
float p ;
p = x * x ;
return ( p ) ;
}
int myfunction(int argc, char *argv[]){
float m, n ;
m=5;
n = square(m);
printf ( "\nSquare of the given number %f is %f",m,n );
}
>> mex myfunction.c
Building with 'MinGW64 Compiler (C)'.
D:\TestMexFunction\myfunction.c: In function 'mexFunction':
D:\TestMexFunction\myfunction.c:21:14: warning: implicit declaration of function 'myfunction' [-Wimplicit-function-declaration]
result = myfunction( argc, argv );
^~~~~~~~~~
I am using Matlab 64bit and the code works if I remove the square() function

Accepted Answer

James Tursa
James Tursa on 19 Sep 2019
Edited: James Tursa on 19 Sep 2019
You call myfunction( ) before the compiler has seen a prototype or definition of myfunction( ). Either move your square( ) and myfunction( ) code above the mexFunction( ) code, or put prototypes at the top of your code. E.g., put these lines at the front:
float square ( float x );
int myfunction(int argc, char *argv[]);
When you call a function before the compiler knows about it, the compiler assumes the function returns an int and that the argument types are exactly as listed. I.e., you get no argument type promotion and the function return type might be wrong. This can lead to crashes, or worse a wrong result without you knowing anything went wrong. C lets you get away with this and will compile the code anyway ... C++ will not let you get away with this and will not compile the code.
Bottom line: Always make sure you have function prototypes or definitions appearing in the code before they are used.
Side Note: On earlier versions of MATLAB, the mxArrayToString allocations were not put on the gargabe collection list, so your current code will leak memory on those versions if you run into an error condition.
  2 Comments
shdotcom shdotcom
shdotcom shdotcom on 19 Sep 2019
"Side Note: On earlier versions of MATLAB, the mxArrayToString allocations were not put on the gargabe collection list, so your current code will leak memory on those versions if you run into an error condition."
Do you mean that I have to change mxArrayToString?

Sign in to comment.

More Answers (0)

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!