Error in Passing Structure Pointer to C Function via calllib

3 visualizzazioni (ultimi 30 giorni)
I have a C function ("Fxn1") that accepts a pointer to a structure as an input.
(C)
int32_T Fxn1(const Mystruct *params) { ...fxn content... }
The structure is defined in a header file and contains "uint8_T" & "uint32_T" data types (which I also define).
(C)
1. typedef unsigned char uint8_T;
2. typedef unsigned int uint32_T;
3. typedef struct{ uint8_T v1; uint32_T v2;} Mystruct;
I compiled the function (with associated headers) into a shared object ("lib.so") and have loaded it into Matlab using the "loadlibrary" function.
At this point Matlab generates errors saying: "redefinition of uint8_T/uint32_T". I removed those typedefs from the header (lines #1 & 2 above), and the errors cleared, but there are a ton of warnings, saying:
(M)
Warning: The data type 'error#XXX' used by structure YYY does not exist. The structure may be unusable.
Warnings aside, a call to "libfunctionsview lib" shows the expected input/output arguments:
(M)
Return Type Name Arguments
[lib.pointer, MystructPtr] Centroid (MystructPtr)
I initialized the struct in Matlab and converted it to a pointer with "libpointer".
(M)
st1.v1 = 1;
st1.v2 = 2;
st1_p = libpointer('Mystruct',st1);
I call the C function in Matlab using "calllib"
(M)
[lb_p, st1_p] = calllib('lib','Fxn1',st1_p);
The C function recognizes the input, but when I try to access the structure values (debugging in terminal), the values are wrong.
(C)
printf("v1= %d, v2= %d",params->v1,params->v2)
(expected output: v1= 1, v2= 2)
actual output: v1= 32503, v2= 139234028
Why would the structure content be scrambled when passed by reference? (It looks like the values are memory addresses or something, but I'm not sure why.) Matlab's addStructbyRef function (from the shrlibsample library) seems to handle this without a problem (https://www.mathworks.com/examples/matlab/mw/matlab-ex69873255-pass-pointer-to-structure).
Help appreciated!

Risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by