How to generate MEX files from C code generated by matlab coder, without using the matalb coder

4 次查看(过去 30 天)
I have matlab coder installed, and I have found the
codegen [-options] [files]
command useful for generating MEX files and improving performance. I have put my code on github, but other users do not have matlab coder installed, so they cannot use the above sytax to generate MEX files for their OS.
During creation of the MEX file, as an intermediate step, matlab produces lots of .c and .h files, in multiple folders. Given that the
mex [options ...] file [files ...]
command in matlab does not require the matlab coder, I would like to use it to produce MEX code based on these .c and .h files. This process should then be repeatable for users who do not have matlab coder installed, thus generating MEX files for their OS and matlab version. But I am not sure which c files I should be pointing to, if I should also be pointing to the .h files, if these files need to be modified, etc.
But I cannot find any instructions on how to do this - any help would be much appreciated - thanks.

采纳的回答

Ramtej
Ramtej 2023-8-23
Hi Andrew,
I understand that you are trying to generate mex files from the C code generated by the MATLAB Coder.
The C files generated by matlab coder heavily depends on the functions and definitions defined in header files located in
fullfile(matlabroot,'extern','include')
Quick tip: Copy all the source and header files inside the codegen folder to the current directory.
Please follow the below steps to generate mex files:
  1. Identify the main file (it is not necessarily main.c), in your case it is named as <Name of the function generated using codegen>.c
  2. Ensure all the source and header files are in one directory.
  3. Make that directory as the current directory.
  4. Please add mexFunction to the .c file from step 1 and refer to this example and C Matrix api while writing mexFunction.
  5. Run the mex command by including path to current folder and predefined header folder or any other folder containing header files by using -I (captial - i) argument.
Example:
% CodeGenFunc is the name of the mex function generated using codegen and
% the corressponding code resided in CodeGenFunc.c
% Copy all the .c and .h files generated during codegen to the current
% dirctory and run
mex('CodeGenFunc.c','-I\', '-I\<path to the folder containing predefined headerfiles>' );
Hope this helps!
  3 个评论
Ramtej
Ramtej 2023-8-24
Hi Andrew,
I tried an alternative workaround and it worked. I assume you are using a linux machine
%toy function used
function z = ToyFunction(a)
z = a+rand(1)*5;
end
Please follow the steps below:
  • Run the matlab coder command to produce C files as before:
codegen ToyFunction -args coder.typeof(1.0) -config:lib
  • Go to the folder: codegen/lib/ToyFunction ( You can find interface and examples subfolders here ).
  • Add mexFunction to the ToyFunction.c same as before.
  • Do not copy files from the interface folder ( all the necessary declarations and definitions are already present in the current folder ).
  • Run the below command ( include all the .c files ).
mex('*.c','-I/','-I/home/andrew/matlab/extern/include');
Note: Be careful with the backslash and forwardslash here
Andrew Gibbs
Andrew Gibbs 2023-8-24
编辑:Andrew Gibbs 2023-8-24
This works, thank you!
Also, I found with this example, replacing your final command with
mex *.c -output alt_name
works, and stops matlab from just naming it something random, in this case naming the MEX file alt_name.

请先登录,再进行评论。

更多回答(0 个)

类别

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

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by