C-Code emxArray help

17 views (last 30 days)
Sukram
Sukram on 13 Aug 2014
Commented: Ryan Livingston on 18 Aug 2014
I've got problems to use a function generated by Matlab C-Coder to a C-Library. Below is the example.c and on the end the .m function.
// general c includes
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
// matlab codegen
#include "src/myfunc3/rt_nonfinite.h"
#include "src/myfunc3/rtGetInf.h"
#include "src/myfunc3/rtGetNaN.h"
#include "src/myfunc3/rtwtypes.h"
#include "src/myfunc3/myfunc3_emxAPI.h"
#include "src/myfunc3/myfunc3_emxutil.h"
#include "src/myfunc3/myfunc3.h"
#include "src/myfunc3/myfunc3_initialize.h"
#include "src/myfunc3/myfunc3_terminate.h"
void main(){
double err, val;
emxArray_real_T *x = emxCreate_real_T(1, 3);
x->data[0] = 1;
x->data[1] = 2;
x->data[2] = 3;
myfunc3(x, &val, &err);
printf("Output, %f, Error: %f\n", val, err);
}
// function [value, err] = myfunc3( a ) %#codegen
// if length(a) > 1
// err = 0;
// value = sum(a);
// else
// err = 1;
// value = 0;
// end
// end
I generate the library like that:
gcc -I src/myfunc/ -shared src/myfunc3/myfunc3.c -o myfunc3.a
When I try now to compile my C file
$ gcc -I src/myfunc3/ -o myfunc_bin r3.c myfunc3.a
/tmp/ccrGQSmI.o: In function `main':
r3.c:(.text+0x13): undefined reference to `emxCreate_real_T'
collect2: ld returned 1 exit status
When I add
typedef struct emxArray_real_T
{
double *data;
int *size;
int allocatedSize;
int numDimensions;
boolean_T canFreeData;
} emxArray_real_T;
I got
$ gcc -I src/myfunc3/ -o myfunc_bin r3.c myfunc3.a
r3.c:17:16: error: redefinition of 'struct emxArray_real_T'
src/myfunc3/myfunc3_types.h:19:8: note: originally defined here
r3.c:24:3: error: conflicting types for 'emxArray_real_T'
src/myfunc3/myfunc3_types.h:30:32: note: previous declaration of 'emxArray_real_T' was here
r3.c: In function 'main':
r3.c:31:26: warning: initialization from incompatible pointer type [enabled by default]
r3.c:36:5: warning: passing argument 1 of 'myfunc3' from incompatible pointer type [enabled by default]
src/myfunc3/myfunc3.h:20:13: note: expected 'const struct emxArray_real_T *' but argument is of type 'struct emxArray_real_T *'
Can you say where are my mistakes and what to do to make it work?

Answers (1)

Ryan Livingston
Ryan Livingston on 18 Aug 2014
If you choose the target type to be a static library and click the build button you should be able to see how the library is created. When the popup appears saying that the build is completed, click the "Show report" link to open the compilation report. In that report notice the "Build log" tab along the bottom and open that. That will show how the static library should be created. Notice that many other C files also need to be compiled to create the library aside from myfunc3.c to provide the emxArray utilities.
There should be a .a file (static library) created in the same directory as your generated code. Why not just use that when creating your executable rather than trying to build the generated code yourself?
  1 Comment
Ryan Livingston
Ryan Livingston on 18 Aug 2014
Alternatively, you could set the target to EXE rather than LIB and specify your main function by following these steps:
Then, the executable will be compiled for you by MATLAB Coder and doing so will ensure that all necessary files are included in the compilation step.

Sign in to comment.

Categories

Find more on Build Configuration in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!