Mex Error on Linux - redefinition of typedef
14 views (last 30 days)
Show older comments
I am trying to compile a mex file to interact with a C function in Matlab. My C function contains various typedefs that appear to conflict with Matlab definitions. When I run:
mex myMxFxn.c sourceFxn.c
I get several messages like this:
/jc/myCode/ft1/my_types.h:11: error: redefinition of typedef 'int8_T'
/usr2/matlab/matlab2017a/extern/include/tmwtypes.h:203: note: previous declaration of 'int8_T' was here
/jc/myCode/ft1/my_types.h:12: error: redefinition of typedef 'uint8_T'
/usr2/matlab/matlab2017a/extern/include/tmwtypes.h:218: note: previous declaration of 'uint8_T' was here
(etc...)
My definitions (in my_types.h) are:
typedef signed char int8_T;
typedef unsigned char uint8_T;
etc...
The definitions in tmwtypes.h are as follows:
typedef INT8_T int8_T;
typedef UINT8_T uint8_T;
(etc...)
If I remove my typedefs my C function falls apart. Seems like Matlab and my C code are both heavily dependent on their respective definitions of those types. How can I prevent the conflict (ideally without changing all my C type names)?
0 Comments
Answers (1)
Himanshu
on 24 Oct 2024 at 6:19
Hey,
I had also faced this issue in older versions of MATLAB; this seems to be a bug ithat was fixed in the version R2018b. So I am assuming you were using a version older than that.
The conflict arises from the redefinition of typedefs in your custom header file my_types.h and MATLAB's tmwtypes.h. You can resolve this issue by conditionally including your typedefs only when they are not already defined by MATLAB.
Modify your my_types.h file to conditionally define these types only if they haven't been defined yet. You can use preprocessor directives to check if a type is already defined:cCopy code#ifndef INT8_T, as shown in the code snippet below.
#ifndef INT8_T
typedef signed char int8_T;
#endif
#ifndef UINT8_T
typedef unsigned char uint8_T;
#endif
Hope this helps!
0 Comments
See Also
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!