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

7 次查看(过去 30 天)
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?)

采纳的回答

Michelle Wu
Michelle Wu 2017-8-28
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.
  1 个评论
James Johnson
James Johnson 2017-8-28
编辑:James Johnson 2017-8-28
"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."
That's really the best answer to my question. Thanks! However I'd be interesting in seeing anyone (having a link to) step through the process of taking coder output and generating a mex file from it. Such a process could be a stepping stone to C for a person such as myself. Others have asked very similar questions.
It might also be useful in the case that coder runs into issues or makes mistakes that human might be able to step in to fix. For example, the built in compiler for windows does not enable parallelization or CUDA acceleration in the generated C files or mex. A person might save themselves time if they first generated the C code without those and then added them in and using the GCC compiler which does support those (compared to writing it from scratch).

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Simulink Coder 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by