Clear Filters
Clear Filters

Generate only 2 files for c++ matlab coder

21 views (last 30 days)
Hi, I'm just getting started with matlab coder to create c++ code from my matlab function. I would like to have just 2 files created for each matlab function, a .cpp file and a .h file, but i'm getting many files generated and i don't understand the usage of those files. My matlab function is simply adding 2 numbers and is called myfunc.m. Matlab coder creates 4 files : myfunc.cpp, myfunc.h ,myfunc_types.h, rtwtypes.h. Moreover, if i try to implement the c++ code in an external c++ project, for example creating a main.cpp function in visual studio code that includes the myfunc.h and calls the function, i always get the error "cannot open source file "tmwtypes.h" ". Looking at the code I can see that at the end of the file rtwtypes.h there is an #include of the tmwtypes.h, obviously the code doesn't find it cause matlab didn't create that file. To use the function in my cpp project i should delete that include and then everything works, but I would like to understand why are they created and how to force matlab to just create the myfunc.cpp and myfunc.h. I leave attached the matlab function i used and the generated c++ files from matlab coder. Hope that you can help me, thanks.

Accepted Answer

Harimurali
Harimurali on 30 Apr 2024
Edited: Harimurali on 30 Apr 2024
Hi Francesco,
When you use MATLAB Coder to generate C++ code from MATLAB functions, it often produces more files than just the CPP and H files for the function itself. The following are the files generated by MATLAB coder:
  • "myfunc.cpp" and "myfunc.h": These are the direct translations of your MATLAB function into C++ code and its declaration, respectively.
  • "myfunc_types.h": This file defines custom types used by your function. Even if your function is simple, MATLAB Coder generates this file to handle potential complexities and custom types more efficiently.
  • "rtwtypes.h" Contains type definitions (similar to "stdint.h" in standard C) ensuring that the code maintains compatibility across different platforms by fixing the size of integer and real types.
The error you are encountering with "tmwtypes.h" not being found is because this file is part of the MATLAB environment, not your generated code. It is expected to be available in the MATLAB installation directories (C:\Program Files\MATLAB\R20xyz\extern\include\tmwtypes.h), not directly with your generated code. This file is part of the MATLAB Runtime library and provides definitions for MATLAB's fundamental types (like "mwArray"). It is necessary for code that closely interacts with MATLAB's data types or needs to ensure type compatibility with MATLAB.
To resolve the "tmwtypes.h" error, you need to ensure that your external C++ project knows where to find the MATLAB include files. This can be done in Visual Studio Code by modifying the "incudePath" in your "c_cpp_properties.json" file to include the path to the MATLAB headers.
MATLAB Coder does not provide an option to generate only two files (CPP and H) without the additional type and runtime support files because those are essential for ensuring the code runs correctly and is compatible across different platforms and MATLAB versions.
Please refer to the following documentation for more information on code generation using MATLAB coder: https://in.mathworks.com/help/releases/R2024a/coder/gs/generating-c-code-from-matlab-code-using-the-matlab-coder-project-interface.html
  1 Comment
Francesco Brescia
Francesco Brescia on 4 May 2024
Thank you Harimurali for your answer and clear explanation, now i understand better the usage of those files. I'll make sure to add the directory in the includepath of my project! Thank you again

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!