Clear Filters
Clear Filters

Run C/C++ code that uses makefile in MATLAB / call via matlab

10 views (last 30 days)
Hi everyone,
I am trying to intergrate the functionality of this C/C++ code: into my MATLAB program. This code is initialised by running make and has dependencies on the GSL package. Does anyone know how I could call this function in MATLAB (2022b or 2023a)?
I have tried to use the coder.ceval function avialable, but I only got it working for simple cases. The requirements of make and the GSL package has made this very difficult to understand how to implement it.
I want to change how the inputs are recieved so a text file of inputs are not needed and these parameters are provided directly from my already made MATLAB program. However, there isn't much point changing the code if I can't get it to run / build in MATLAB.
Any advice on what to try next would be amazing.
Thank you in advance for any possible help,

Answers (1)

Jack on 29 Mar 2023
Integrating external C/C++ code with MATLAB can be challenging, but it is possible to call C functions from MATLAB using the MATLAB Coder toolbox. Here are some steps to try:
  1. Install the MATLAB Coder toolbox if it is not already installed.
  2. Modify the C/C++ code to expose the desired functionality as a C function with C linkage.
  3. Compile the modified C/C++ code into a shared library or DLL using the MATLAB Coder toolbox. You will need to include the GSL library as part of the compilation process.
  4. In MATLAB, use the loadlibrary function to load the shared library or DLL containing the C function.
  5. Use the calllib function to call the C function from MATLAB, passing in any necessary arguments and retrieving any return values.
It sounds like you have already attempted step 4 using the coder.ceval function, which is similar to calllib but designed for use with the MATLAB Coder toolbox. However, if you are encountering issues with dependencies and linking, it may be helpful to try compiling the code into a shared library or DLL as described in step 3.
Keep in mind that modifying and compiling external code requires a solid understanding of both the original code and the MATLAB Coder toolbox. It may be helpful to consult with a MATLAB expert or reach out to the code authors for assistance.


Find more on C Shared Library Integration in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!