Clear Filters
Clear Filters

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

2 views (last 30 days)
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.

Accepted Answer

Ramtej
Ramtej on 23 Aug 2023
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 Comments
Ramtej
Ramtej on 24 Aug 2023
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 on 24 Aug 2023
Edited: Andrew Gibbs on 24 Aug 2023
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.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!