Code Generation folder for C/C++ code block

Hello,
I am using the C/C++ code block to include some custome C code in my model. I am buiilding the code by using the "Build" button pictured below:
This works fine and produces a C source file.
My issue is when I CodeGen I have to manually include this Custom Code file in the folder each time (it gets deleted otherwise).
I am curious is there a way to control where this C/C++ codeblock builds its file and how I can have that build location to be the same as CodeGeneration ? Right now the C/C++ function always builds to the root directory of my simulink project.

3 Comments

Hi Jake,
In the "Specify Custom Code Settings for Code Generation" section of above link, you will find that there are options available like "Include directories", "Source files*" etc.
Is the "C/C++ code block" part of the same Simulink model for which you are generating code using "Simulink Coder"?
Hello Piyush,
Thanks for your comment.
The C/C++ block is part of my model, I am generating code using embedded coder.
The generated code references the custom code just fine, I would just like to avoid the manual process of having the place the custom code in the CodeGen folder each time.
The requirement for my project is all the neccessarry code be placed in a specific folder, it does not work to just have include paths to the directory.
and to be clear all the custom code is already included inside the C function block.
You do not need to place the custom code in the CodeGen folder ... in fact you should not place it there, as it will get deleted when you regenerate code for your model. Place the .c and .h files in a different folder and then:
  1. In the "Include Directories" section of the custom code, specify the path to your header file
  2. In the "Source Files" section, specify the full/relative path to your C files ( for example c:/work/src/ABC.c )
Thanks.
Mark.

Sign in to comment.

 Accepted Answer

Abhas
Abhas on 3 Dec 2024
Edited: Abhas on 3 Dec 2024
To handle your problem and automate the inclusion of your custom code during code generation in Simulink using Embedded Coder, follow the below workflow:
  • Go to "Model Configuration Parameters" (Ctrl + E or through the Simulation menu).
  • Navigate to "Code Generation > Custom Code".
  • Specify the paths and files by adding the folder path for your .h files (header files) in the "Include Directories", the relative or absolute paths to your .c files in the "Source Files", and listing any custom libraries needed for your project.
  • Ensure your .c and .h files are in a permanent location (not in the CodeGen folder). Example: "C:/Projects/MySimulinkCode".
  • The Simulink Coder will automatically reference the paths provided in the "Custom Code" section without requiring manual file placement.
You may refer the below documentation links to know more about the same:
  1. https://www.mathworks.com/help/rtw/ug/external-code-integration-examples.html
  2. https://www.mathworks.com/help/plccoder/ug/generate-custom-code-by-using-ide-specific-callback-functions.html
  3. https://www.mathworks.com/help/simulink/ug/specify-configure-custom-c-code.html#mw_ac59cae5-336f-43f8-9b07-fbdcbc500b57
I hope this helps!

More Answers (0)

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products

Release

R2024a

Asked:

on 24 Apr 2024

Edited:

on 3 Dec 2024

Community Treasure Hunt

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

Start Hunting!