Create dll libraries for C++ multiple functions in a script

49 views (last 30 days)
Hello,
I have been testing with matlab coder tool in order to create a dll library for some functions I have saved in one script. So far, I dont understand very well how does this work, cause coder creates a folder with multiple scripts and files which very confusing for me. My problem is this:
I'm translating a code from matlab to cpp but because of this functions are quite long with large numbers, I wouldl like to convert them into a dll and then include it in my cpp project.
Sorry if its simple but I'm new to cpp so its being quite hard for me to understand.
First, is this a good approach to solve the problem?
Second, is possible to save the script in one dll?
Attached my script
Thanks in advance for helping!

Accepted Answer

Shubham
Shubham on 6 Mar 2023
Edited: Shubham on 6 Mar 2023
Hello Mikel,
Regarding your first question, creating a DLL (Dynamic Link Library) is a good approach to solve your problem if you want to reuse your MATLAB code in a C++ project. By doing so, you can create a bridge between the two languages, allowing you to call your MATLAB functions from your C++ code.
As for your second question, you cannot simply save your MATLAB script as a DLL. The MATLAB Coder generates C++ code from your MATLAB code, which you then compile into a DLL. This generated C++ code will contain multiple files, including header files, source files, and a Visual Studio project file.
I recommend that you start by generating the C++ code using MATLAB Coder, then compiling it into a DLL using Visual Studio (or another C++ compiler). Here's a brief overview of the steps involved:
  1. Open MATLAB and load your MATLAB code.
  2. Use the MATLAB Coder app or the command line interface to generate C++ code from your MATLAB code. This will create a folder containing the generated C++ files.
  3. Open the Visual Studio project file in the generated folder.
  4. Build the project to create a DLL.
  5. Once you have the DLL, you can link it to your C++ project and call your MATLAB functions as if they were native C++ functions.
However, keep in mind that not all MATLAB functions can be easily converted to C++ functions. For example, some MATLAB functions use data types that are not supported in C++. Also, depending on the complexity of your code, you may need to manually edit the generated C++ code to make it more suitable for your needs.
I hope this helps!

More Answers (0)

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!