MATLAB: How to build mex files from the C code which is output by coder

accelerrated codematlab codermex compiler

Hello,
When using the "coder" app to generate mex files directly the result is still too slow. However when one uses the coder app to generate C code they are given the option to optimize for code speed rather than build time (this option is not available for mex files). So I hypothesize that when building mex files directly "coder" optimizes build time rather than speed and that's why it is slow. Hence I would like to build a mex file from the C code.
However the help files for the "mex" function are no help and neither are the help pages purporting to show compiling a mex file from C source files (the situations described are not similar enough). I clicked the option to generate only one C file but it still produces many. Even when I include all of the C files it wont compile. I think I need to include other files, such as .h files or files from the "interface" directory.
The function I am trying to mex is the the "autocov_to_var" function from the MVGC toolbox: <http://users.sussex.ac.uk/~lionelb/MVGC/>
When I run coder to generate C files and tell it to produce only 1 C file the result is the directory below:
From that directory I tried the call:
mex autocov_to_var.c rt_nonfinite.c rtGetInf.c rtGetNaN.c
and got:
Building with 'MinGW64 Compiler (C)'. Error using mex Cannot export mexFunction: symbol not defined collect2.exe: error: ld returned 1 exit status
What files do I need to include in my call to mex? (Is there a better way to ensure my mex file is as fast as humanly (err MatLab-anly) possible?)

Best Answer

The following error:
Error using mex
Cannot export mexFunction: symbol not defined
collect2.exe: error: ld returned 1 exit status
means that function 'mexfunction' is missing from the C code that you are trying to generate MEX from. Just as every C program has a main() function, MATLAB uses the gateway routine 'mexfunction' as the entry point to the function. For more information on 'mexfunction', please refer to the following documentation:
Because the C code generated from MATLAB Coder is usually not ready to be 'mex'-ed, you may need to do a lot of post-processing to mex the C code successfully (such as adding the entry point function).
There is no option to optimize code speed when generating MEX with MATLAB Coder because MEX files are always implicitly optimized for speed as they are able to call MEX function libraries.
Related Question