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

22 次查看(过去 30 天)
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

采纳的回答

James Tursa
James Tursa 2019-9-19
编辑:James Tursa 2019-9-19
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 个评论
shdotcom shdotcom
shdotcom shdotcom 2019-9-19
"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?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by