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

조회 수: 27 (최근 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
MathWorks Support Team 2024년 5월 1일
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