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

6 次查看(过去 30 天)

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?

采纳的回答

MathWorks Support Team
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:

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Simulink Coder 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by