Clear Filters
Clear Filters

Why do I get "undefined reference to variable" and "ld returned 1 exit status" when using custom code in my Simulink model?

5 views (last 30 days)

I have a Simulink model with custom code mappings. I have configured the code mappings to use an imported header file that contains definitions for all of my custom variables. These variables are defined in a corresponding C file, and both files are added to the "Include Directories" section under the Configuration Parameters "Custom Code" tab.

I can generate code without issue, but if I try to build with the "Generate code only" (GenCodeOnly) option disabled, or run the model in a harness like Simulink Test, I get errors that look like this:

file.c:(.segment.table[varname]): undefined reference to `varname'
...
collect2: error: ld returned 1 exit status
project.mk:123: recipe for target 'project' failed
make: *** [project] Error 1
Why is this happening, and how do I fix these errors?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 1 May 2024
Due to limitations of the C and C++ languages, if you are providing outside code to Simulink that is not generated during the current build process, you must specify the source (.c or .cpp) files as well as the header files (.h or .hpp).
In the "Code Generation > Custom Code" section, enter the file names of any source files in the "Source Files" box. If you are specifying multiple files, separate them with spaces or line breaks. If the files are in a separate folder, you will also need to enter the path to that folder under the adjacent "Include Directories" tab.
For example, if you define your variables in the files "mySource1.c" and "mySource2.c" inside the current folder, enter these file names in the "Source files" box:

More Answers (0)

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!