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

27 ビュー (過去 30 日間)
shdotcom shdotcom
shdotcom shdotcom 2019 年 9 月 19 日
コメント済み: shdotcom shdotcom 2019 年 9 月 19 日
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 日
Thank you very much
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 Center および File ExchangeWrite 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