How to structure a large MATLAB Project with custom MEX Files?

I work on a large MATLAB Codebase and for performance reasons we had to rewrite some functions in c++, compile them into a MEX File and then run them instead of the MATLAB Functions.
Now I store the MATLAB Code, the c++ Code and the MEX Files together in the same repository. This approach works, but is cumbersome. If I change something in the c++ Code I have to compile it by hand and then copy the new MEX File into the right spot. This approach makes debugging a nightmare and is also a bad practice as you shouldn't store artifacts together with the corresponding code.
Ideally I would like to change something in the c++ code, test it without manual compiling and then commit just the changed code.
Is there a best practice for this kind of scenario? Currently I use cmake for the mex files because of dependency management. And I don't know of any just in time mechanism of compiling the MEX Files via cmake from MATLAB.

Answers (1)

Some advices:
  1. Might not be a good idea to store the MEX files in the repo. Use a build machine dedicated to compile. Then your MEX files can always be regenerated when needed
  2. Use unittest to test your C++ code. It is just C++ code, create C++ unite tests to test them.
  3. Store those unit tests in your repo as well.

2 Comments

The Problem is, that the MEX Files have to be somewhere for them to be called from MATLAB. Is it really a better Idea to setup a whole CI/CD Pipeline and Artifactory than just to compile the code and store the mex files until the code changes? Seems a little bit Overkill for what I assume can be fixed with a little bit of reorganisation.
Put this in your build machine script. The build script shall
  1. Compile the C++ code and generate MEX files
  2. Move the MEX files to the correct locations

Sign in to comment.

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Products

Release

R2020b

Asked:

on 3 Aug 2021

Commented:

on 3 Aug 2021

Community Treasure Hunt

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

Start Hunting!