MATLAB Answers

Error using mex G:\Matlab_​WS\code_te​st\Assignm​ent1\Add_T​wo.c:3:1: warning: return type defaults to 'int' [-Wimplicit-int] Add_Two(U16 a, U16 b)

21 views (last 30 days)
Trying to understand mex compilation with an example, but getting compilation error. Below are the commands that I used to compile. Mex file is generated but its compilation failed.
def = legacy_code('initialize');
def.SourceFiles = {'Add_Two.c'};
def.HeaderFiles = {'Add_Two.h'};
def.SFunctionName = 'Add_Two_SFun';
def.OutputFcnSpec = 'uint32 y1 = Add_Two(uint16 u1, uint16 u2)';
legacy_code('sfcn_cmex_generate', def);
>> legacy_code('compile', def)
### Start Compiling Add_Two_SFun
mex('-IG:\Matlab_WS\code_test\Assignment1', '-c', '-outdir', 'C:\Users\Shafi\AppData\Local\Temp\tp548d5365_ef4a_45c6_9aec_e200ba5a55ff', 'G:\Matlab_WS\code_test\Assignment1\Add_Two.c')
Building with 'MinGW64 Compiler (C)'.
Error using mex
G:\Matlab_WS\code_test\Assignment1\Add_Two.c:3:1: warning: return type defaults to 'int'
[-Wimplicit-int]
Add_Two(U16 a, U16 b)
^
G:\Matlab_WS\code_test\Assignment1\Add_Two.c:3:1: error: conflicting types for 'Add_Two'
In file included from G:\Matlab_WS\code_test\Assignment1\Add_Two.c:1:0:
G:\Matlab_WS\code_test\Assignment1/Add_Two.h:11:5: note: previous declaration of 'Add_Two' was here
U32 Add_Two(U16 a, U16 b);
^
Error in legacycode.LCT/compile
Error in legacycode.LCT.legacyCodeImpl
Error in legacy_code (line 101)
[varargout{1:nargout}] = legacycode.LCT.legacyCodeImpl(action, varargin{1:end});
***************************************************
Input files:
Add_Two.c:
#include <Add_Two.h>
Add_Two(U16 a, U16 b)
{
U32 add_result;
U32 output;
add_result = (U32)a + (U32)b;
if(THOUSAND > add_result)
{
output = add_result;
}
else
{
output = SUM_DEFAULT;
}
return output;
}
Add_Two.h:
#ifndef ADD_TWO_H_
#define ADD_TWO_H_
typedef unsigned int U16;
typedef unsigned long int U32;
#define THOUSAND (U32)1000
#define SUM_DEFAULT (U32)25
U32 Add_Two(U16 a, U16 b);
#endif

Accepted Answer

Andrew Janke
Andrew Janke on 31 Jan 2020
Edited: Andrew Janke on 31 Jan 2020
See this line where the function is actually defined in Add_Two.c?
Add_Two(U16 a, U16 b)
You need to add an explicit "`U32`" as its return type there.
U32 Add_Two(U16 a, U16 b)
  2 Comments
Andrew Janke
Andrew Janke on 31 Jan 2020
Heheh. Happens to the best of us.
You need to keep that function declaration in there: because it is returning something besides a regular int, you need the explicit prototype in there to tell the C compiler what type of value to expect coming back from the function.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!