Error "undefined reference to `my_alg(double)'" by generating code for example externalcode

14 views (last 30 days)
Dear all,
I´m getting frustrated by testing out the given example for a TLC wrapper from this documentation page:
The command to open the example from MATLAB command line is:
cd(fullfile(matlabroot,'/toolbox/rtw/rtwdemos/tlctutorial/wrapper'))
Before generating the code I saved the old "externalcode.mdl" as "externalcode.slx". I´m using the Build button under the "Real-Time" ribbon. I did not define anything in the Configuration Parameter -> Code generation -> Custom Code section by hand. Since I´m using the solution files from the example my expectation is, that everything should be correctly configured work out of the box. What is my code missing? Any help highly appreciated.
This is the error I´m getting from the Diagnostic viewer:
### Invoking custom build hook: CodeGenEntry
### Generating code and artifacts to 'Model specific' folder structure
### Generating code into build folder: C:\eSE\Test\eSE\ModSys\sfunctions\wrapper\externalcode_slrealtime_rtw
### Invoking custom build hook: CodeGenBeforeTLC
### Invoking Target Language Compiler on externalcode.rtw
### Using System Target File: C:\Program Files\MATLAB\R2020b\toolbox\slrealtime\simulink\coder\rtw\slrealtime.tlc
### Loading TLC function libraTest
### Generating TLC interface API for custom data
### Initial pass through model to cache user defined code
.
### Caching model source code
### Writing header file externalcode_types.h
.
### Writing header file externalcode.h
### Writing header file rtwtypes.h
### Writing header file multiword_types.h
### Writing header file externalcode_cal.h
### Writing source file externalcode.cpp
### Writing header file externalcode_private.h
### Writing header file rtmodel.h
.
### Writing source file externalcode_cal.cpp
### TLC code generation complete.
### Creating slrealtime_task_info.m
### Invoking custom build hook: CodeGenAfterTLC
### Invoking custom build hook: CodeGenBeforeMake
### Using toolchain: Simulink Real-Time Toolchain
### Creating 'C:\eSE\Test\eSE\ModSys\sfunctions\wrapper\externalcode_slrealtime_rtw\instrumented\externalcode.mk' ...
### Building 'externalcode': make -f externalcode.mk all
C:\eSE\Test\eSE\ModSys\sfunctions\wrapper\externalcode_slrealtime_rtw\instrumented>call "C:\ProgramData\MATLAB\SupportPackages\R2020b\toolbox\slrealtime\target\supportpackage\qnx700\qnxsdp-env.bat"
C:\eSE\Test\eSE\ModSys\sfunctions\wrapper\externalcode_slrealtime_rtw\instrumented>set QNX_BASE=C:/ProgramData/MATLAB/SupportPackages/R2020b/toolbox/slrealtime/target/supportpackage/qnx700/
C:\eSE\Test\eSE\ModSys\sfunctions\wrapper\externalcode_slrealtime_rtw\instrumented>set QNX_TARGET=C:/ProgramData/MATLAB/SupportPackages/R2020b/toolbox/slrealtime/target/supportpackage/qnx700/target/qnx7
C:\eSE\Test\eSE\ModSys\sfunctions\wrapper\externalcode_slrealtime_rtw\instrumented>set QNX_HOST=C:/ProgramData/MATLAB/SupportPackages/R2020b/toolbox/slrealtime/target/supportpackage/qnx700/host/win64/x86_64
C:\eSE\Test\eSE\ModSys\sfunctions\wrapper\externalcode_slrealtime_rtw\instrumented>set QNX_CONFIGURATION_EXCLUSIVE=C:\Users\Test.ELENIA.004/.qnx
C:\eSE\Test\eSE\ModSys\sfunctions\wrapper\externalcode_slrealtime_rtw\instrumented>set QNX_CONFIGURATION=C:\Users\Test.ELENIA.004/.qnx
C:\eSE\Test\eSE\ModSys\sfunctions\wrapper\externalcode_slrealtime_rtw\instrumented>set MAKEFLAGS=-IC:/ProgramData/MATLAB/SupportPackages/R2020b/toolbox/slrealtime/target/supportpackage/qnx700/target/qnx7/usr/include
C:\eSE\Test\eSE\ModSys\sfunctions\wrapper\externalcode_slrealtime_rtw\instrumented>set PATH=C:/ProgramData/MATLAB/SupportPackages/R2020b/toolbox/slrealtime/target/supportpackage/qnx700/host/win64/x86_64/usr/bin;C:/ProgramData/MATLAB/SupportPackages/R2020b/toolbox/slrealtime/target/supportpackage/qnx700/jre/bin;C:\Program Files\MATLAB\R2020b\bin\win64;C:\Program Files\Python39\Scripts\;C:\Program Files\Python39\;C:\Program Files\Common Files\Siemens\Automation\Simatic OAM\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\iCLS\;C:\Program Files\Intel\Intel(R) Management Engine Components\iCLS\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\MATLAB\R2020b\runtime\win64;C:\Program Files\MATLAB\R2020b\bin;C:\Program Files\Microsoft VS Code\bin;C:\Program Files\Git\cmd;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\IVI Foundation\VISA\Win64\Bin\;C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Bin\;C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Bin;C:\Users\Test.ELENIA.000.001.002.003.004\AppData\Local\Programs\Python\Python38\Scripts\;C:\Users\Test.ELENIA.000.001.002.003.004\AppData\Local\Programs\Python\Python38\;C:\Users\Test\AppData\Local\Programs\Python\Python37-32\Scripts\;C:\Users\Test\AppData\Local\Programs\Python\Python37-32\;C:\Users\Test.ELENIA.004\AppData\Local\Microsoft\WindowsApps;C:\texlive\2018\bin\win32;C:\Users\Test\AppData\Local\Programs\Microsoft VS Code\bin;
C:\eSE\Test\eSE\ModSys\sfunctions\wrapper\externalcode_slrealtime_rtw\instrumented>set TMPDIR=C:\Users\TestEL~1.004\AppData\Local\Temp
C:\eSE\Test\eSE\ModSys\sfunctions\wrapper\externalcode_slrealtime_rtw\instrumented>REM Checking license
C:\eSE\Test\eSE\ModSys\sfunctions\wrapper\externalcode_slrealtime_rtw\instrumented>set QNX_LICENSE_FILE_LOCATION=C:\Users\Test.ELENIA.004\.qnx\license\
C:\eSE\Test\eSE\ModSys\sfunctions\wrapper\externalcode_slrealtime_rtw\instrumented>set QNX_LICENSE_FILE=C:\Users\Test.ELENIA.004\.qnx\license\licenses
C:\eSE\Test\eSE\ModSys\sfunctions\wrapper\externalcode_slrealtime_rtw\instrumented>set QNX_LICENSE_SOURCE_FILE=C:\ProgramData\MATLAB\SupportPackages\R2020b\toolbox\slrealtime\target\supportpackage\qnx700\license\licenses
C:\eSE\Test\eSE\ModSys\sfunctions\wrapper\externalcode_slrealtime_rtw\instrumented>IF NOT EXIST C:\Users\Test.ELENIA.004\.qnx\license\licenses (xcopy C:\ProgramData\MATLAB\SupportPackages\R2020b\toolbox\slrealtime\target\supportpackage\qnx700\license\licenses C:\Users\Test.ELENIA.004\.qnx\license\ /s /i /Y )
C:\eSE\Test\eSE\ModSys\sfunctions\wrapper\externalcode_slrealtime_rtw\instrumented>cd .
C:\eSE\Test\eSE\ModSys\sfunctions\wrapper\externalcode_slrealtime_rtw\instrumented>chcp 1252
Aktive Codepage: 1252.
C:\eSE\Test\eSE\ModSys\sfunctions\wrapper\externalcode_slrealtime_rtw\instrumented>if "" == "" (make -f externalcode.mk all ) else (make -f externalcode.mk )
qcc -c -Vgcc_ntox86_64 -g -O2 -ffast-math -fwrapv -DSIMULINK_REAL_TIME -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DEXT_MODE=1 -DMAT_FILE=0 -DONESTEPFCN=1 -DTERMFCN=1 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=1 -DMODEL=externalcode -DNUMST=2 -DNCSTATES=0 -DHAVESTDIO -DRT -DUSE_RTMODEL -IC:/eSE/Test/eSE/ModSys/sfunctions/wrapper -IC:/eSE/Test/eSE/ModSys/sfunctions/wrapper/externalcode_slrealtime_rtw -IC:/PROGRA~1/MATLAB/R2020b/extern/include -IC:/PROGRA~1/MATLAB/R2020b/simulink/include -IC:/PROGRA~1/MATLAB/R2020b/rtw/c/src -IC:/PROGRA~1/MATLAB/R2020b/rtw/c/src/ext_mode/common -IC:/PROGRA~1/MATLAB/R2020b/toolbox/slrealtime/target/kernel/include -IC:/PROGRA~1/MATLAB/R2020b/toolbox/slrealtime/simulink/blocks/dist/include -IC:/PROGRA~1/MATLAB/R2020b/toolbox/slrealtime/target/kernel/dist/include -o slrealtime_datatype_ground.o C:/eSE/Test/eSE/ModSys/sfunctions/wrapper/externalcode_slrealtime_rtw/slrealtime_datatype_ground.c
q++ -c -Vgcc_ntox86_64 -g -std=gnu++14 -stdlib=libstdc++ -O2 -ffast-math -fwrapv -DSIMULINK_REAL_TIME -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DEXT_MODE=1 -DMAT_FILE=0 -DONESTEPFCN=1 -DTERMFCN=1 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=1 -DMODEL=externalcode -DNUMST=2 -DNCSTATES=0 -DHAVESTDIO -DRT -DUSE_RTMODEL -IC:/eSE/Test/eSE/ModSys/sfunctions/wrapper -IC:/eSE/Test/eSE/ModSys/sfunctions/wrapper/externalcode_slrealtime_rtw -IC:/PROGRA~1/MATLAB/R2020b/extern/include -IC:/PROGRA~1/MATLAB/R2020b/simulink/include -IC:/PROGRA~1/MATLAB/R2020b/rtw/c/src -IC:/PROGRA~1/MATLAB/R2020b/rtw/c/src/ext_mode/common -IC:/PROGRA~1/MATLAB/R2020b/toolbox/slrealtime/target/kernel/include -IC:/PROGRA~1/MATLAB/R2020b/toolbox/slrealtime/simulink/blocks/dist/include -IC:/PROGRA~1/MATLAB/R2020b/toolbox/slrealtime/target/kernel/dist/include -o externalcode.o C:/eSE/Test/eSE/ModSys/sfunctions/wrapper/externalcode_slrealtime_rtw/externalcode.cpp
q++ -c -Vgcc_ntox86_64 -g -std=gnu++14 -stdlib=libstdc++ -O2 -ffast-math -fwrapv -DSIMULINK_REAL_TIME -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DEXT_MODE=1 -DMAT_FILE=0 -DONESTEPFCN=1 -DTERMFCN=1 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=1 -DMODEL=externalcode -DNUMST=2 -DNCSTATES=0 -DHAVESTDIO -DRT -DUSE_RTMODEL -IC:/eSE/Test/eSE/ModSys/sfunctions/wrapper -IC:/eSE/Test/eSE/ModSys/sfunctions/wrapper/externalcode_slrealtime_rtw -IC:/PROGRA~1/MATLAB/R2020b/extern/include -IC:/PROGRA~1/MATLAB/R2020b/simulink/include -IC:/PROGRA~1/MATLAB/R2020b/rtw/c/src -IC:/PROGRA~1/MATLAB/R2020b/rtw/c/src/ext_mode/common -IC:/PROGRA~1/MATLAB/R2020b/toolbox/slrealtime/target/kernel/include -IC:/PROGRA~1/MATLAB/R2020b/toolbox/slrealtime/simulink/blocks/dist/include -IC:/PROGRA~1/MATLAB/R2020b/toolbox/slrealtime/target/kernel/dist/include -o externalcode_cal.o C:/eSE/Test/eSE/ModSys/sfunctions/wrapper/externalcode_slrealtime_rtw/externalcode_cal.cpp
qcc -c -Vgcc_ntox86_64 -g -O2 -ffast-math -fwrapv -DSIMULINK_REAL_TIME -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DEXT_MODE=1 -DMAT_FILE=0 -DONESTEPFCN=1 -DTERMFCN=1 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=1 -DMODEL=externalcode -DNUMST=2 -DNCSTATES=0 -DHAVESTDIO -DRT -DUSE_RTMODEL -IC:/eSE/Test/eSE/ModSys/sfunctions/wrapper -IC:/eSE/Test/eSE/ModSys/sfunctions/wrapper/externalcode_slrealtime_rtw -IC:/PROGRA~1/MATLAB/R2020b/extern/include -IC:/PROGRA~1/MATLAB/R2020b/simulink/include -IC:/PROGRA~1/MATLAB/R2020b/rtw/c/src -IC:/PROGRA~1/MATLAB/R2020b/rtw/c/src/ext_mode/common -IC:/PROGRA~1/MATLAB/R2020b/toolbox/slrealtime/target/kernel/include -IC:/PROGRA~1/MATLAB/R2020b/toolbox/slrealtime/simulink/blocks/dist/include -IC:/PROGRA~1/MATLAB/R2020b/toolbox/slrealtime/target/kernel/dist/include -o my_alg.o C:/eSE/Test/eSE/ModSys/sfunctions/wrapper/my_alg.c
q++ -c -Vgcc_ntox86_64 -g -std=gnu++14 -stdlib=libstdc++ -O2 -ffast-math -fwrapv -DSIMULINK_REAL_TIME -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DEXT_MODE=1 -DMAT_FILE=0 -DONESTEPFCN=1 -DTERMFCN=1 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=1 -DMODEL=externalcode -DNUMST=2 -DNCSTATES=0 -DHAVESTDIO -DRT -DUSE_RTMODEL -IC:/eSE/Test/eSE/ModSys/sfunctions/wrapper -IC:/eSE/Test/eSE/ModSys/sfunctions/wrapper/externalcode_slrealtime_rtw -IC:/PROGRA~1/MATLAB/R2020b/extern/include -IC:/PROGRA~1/MATLAB/R2020b/simulink/include -IC:/PROGRA~1/MATLAB/R2020b/rtw/c/src -IC:/PROGRA~1/MATLAB/R2020b/rtw/c/src/ext_mode/common -IC:/PROGRA~1/MATLAB/R2020b/toolbox/slrealtime/target/kernel/include -IC:/PROGRA~1/MATLAB/R2020b/toolbox/slrealtime/simulink/blocks/dist/include -IC:/PROGRA~1/MATLAB/R2020b/toolbox/slrealtime/target/kernel/dist/include -o externalcode_calintf.o C:/eSE/Test/eSE/ModSys/sfunctions/wrapper/externalcode_slrealtime_rtw/externalcode_calintf.cpp
q++ -c -Vgcc_ntox86_64 -g -std=gnu++14 -stdlib=libstdc++ -O2 -ffast-math -fwrapv -DSIMULINK_REAL_TIME -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DEXT_MODE=1 -DMAT_FILE=0 -DONESTEPFCN=1 -DTERMFCN=1 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=1 -DMODEL=externalcode -DNUMST=2 -DNCSTATES=0 -DHAVESTDIO -DRT -DUSE_RTMODEL -IC:/eSE/Test/eSE/ModSys/sfunctions/wrapper -IC:/eSE/Test/eSE/ModSys/sfunctions/wrapper/externalcode_slrealtime_rtw -IC:/PROGRA~1/MATLAB/R2020b/extern/include -IC:/PROGRA~1/MATLAB/R2020b/simulink/include -IC:/PROGRA~1/MATLAB/R2020b/rtw/c/src -IC:/PROGRA~1/MATLAB/R2020b/rtw/c/src/ext_mode/common -IC:/PROGRA~1/MATLAB/R2020b/toolbox/slrealtime/target/kernel/include -IC:/PROGRA~1/MATLAB/R2020b/toolbox/slrealtime/simulink/blocks/dist/include -IC:/PROGRA~1/MATLAB/R2020b/toolbox/slrealtime/target/kernel/dist/include -o main.o C:/eSE/Test/eSE/ModSys/sfunctions/wrapper/externalcode_slrealtime_rtw/main.cpp
qcc -c -Vgcc_ntox86_64 -g -O2 -ffast-math -fwrapv -DSIMULINK_REAL_TIME -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DEXT_MODE=1 -DMAT_FILE=0 -DONESTEPFCN=1 -DTERMFCN=1 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=1 -DMODEL=externalcode -DNUMST=2 -DNCSTATES=0 -DHAVESTDIO -DRT -DUSE_RTMODEL -IC:/eSE/Test/eSE/ModSys/sfunctions/wrapper -IC:/eSE/Test/eSE/ModSys/sfunctions/wrapper/externalcode_slrealtime_rtw -IC:/PROGRA~1/MATLAB/R2020b/extern/include -IC:/PROGRA~1/MATLAB/R2020b/simulink/include -IC:/PROGRA~1/MATLAB/R2020b/rtw/c/src -IC:/PROGRA~1/MATLAB/R2020b/rtw/c/src/ext_mode/common -IC:/PROGRA~1/MATLAB/R2020b/toolbox/slrealtime/target/kernel/include -IC:/PROGRA~1/MATLAB/R2020b/toolbox/slrealtime/simulink/blocks/dist/include -IC:/PROGRA~1/MATLAB/R2020b/toolbox/slrealtime/target/kernel/dist/include -o host_timer_x86.o C:/PROGRA~1/MATLAB/R2020b/toolbox/coder/profile/src/host_timer_x86.c
q++ -c -Vgcc_ntox86_64 -g -std=gnu++14 -stdlib=libstdc++ -O2 -ffast-math -fwrapv -DSIMULINK_REAL_TIME -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DEXT_MODE=1 -DMAT_FILE=0 -DONESTEPFCN=1 -DTERMFCN=1 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=1 -DMODEL=externalcode -DNUMST=2 -DNCSTATES=0 -DHAVESTDIO -DRT -DUSE_RTMODEL -IC:/eSE/Test/eSE/ModSys/sfunctions/wrapper -IC:/eSE/Test/eSE/ModSys/sfunctions/wrapper/externalcode_slrealtime_rtw -IC:/PROGRA~1/MATLAB/R2020b/extern/include -IC:/PROGRA~1/MATLAB/R2020b/simulink/include -IC:/PROGRA~1/MATLAB/R2020b/rtw/c/src -IC:/PROGRA~1/MATLAB/R2020b/rtw/c/src/ext_mode/common -IC:/PROGRA~1/MATLAB/R2020b/toolbox/slrealtime/target/kernel/include -IC:/PROGRA~1/MATLAB/R2020b/toolbox/slrealtime/simulink/blocks/dist/include -IC:/PROGRA~1/MATLAB/R2020b/toolbox/slrealtime/target/kernel/dist/include -o slrealtime_code_profiling_utility_functions.o C:/eSE/Test/eSE/ModSys/sfunctions/wrapper/externalcode_slrealtime_rtw/slrealtime_code_profiling_utility_functions.cpp
### Creating standalone executable C:/eSE/Test/eSE/ModSys/sfunctions/wrapper/externalcode_slrealtime_rtw/externalcode ...
q++ -Vgcc_ntox86_64 -g -std=gnu++14 -stdlib=libstdc++ -o C:/eSE/Test/eSE/ModSys/sfunctions/wrapper/externalcode_slrealtime_rtw/externalcode slrealtime_datatype_ground.o externalcode.o externalcode_cal.o my_alg.o externalcode_calintf.o main.o host_timer_x86.o slrealtime_code_profiling_utility_functions.o -LC:/PROGRA~1/MATLAB/R2020b/toolbox/slrealtime/target/lib -ltraceparser -lpps -lslrealtime_kernel -lslrealtime_rtps -lsocket -lboost_system -lboost_log -lpci -lopenblas
externalcode.o: In function `externalcode_step':
C:/eSE/Test/eSE/ModSys/sfunctions/wrapper/externalcode_slrealtime_rtw/externalcode.cpp:44: undefined reference to `my_alg(double)'
cc: C:/ProgramData/MATLAB/SupportPackages/R2020b/toolbox/slrealtime/target/supportpackage/qnx700/host/win64/x86_64/usr/bin/x86_64-pc-nto-qnx7.0.0-ld caught signal 1
externalcode.mk:234: recipe for target 'C:/eSE/Test/eSE/ModSys/sfunctions/wrapper/externalcode_slrealtime_rtw/externalcode' failed
make: *** [C:/eSE/Test/eSE/ModSys/sfunctions/wrapper/externalcode_slrealtime_rtw/externalcode] Error 1
C:\eSE\Test\eSE\ModSys\sfunctions\wrapper\externalcode_slrealtime_rtw\instrumented>echo The make command returned an error of 2
The make command returned an error of 2
C:\eSE\Test\eSE\ModSys\sfunctions\wrapper\externalcode_slrealtime_rtw\instrumented>exit 1

Accepted Answer

johnnynoc4sh
johnnynoc4sh on 2 Sep 2021
Edited: johnnynoc4sh on 2 Sep 2021
To answer my own question (it costed me nearly a whole night and day to figure that out):
Because Simulink Coder is using a C++ compiler since 2020b (instead a C compiler like in the previous versions) the linker expects names to be mangled (see https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix/12574420#12574420 ). You can check if your project is compiled as C++ Code by verifying that the file in which the error is mentioned by the Diagnostic viewer has the *.cpp file format ending.
In order to fix the error you need to follow the workaround mentioned on this website:
You have to apply the workaround on your function declaration in the S-Function code file (wrapsfcn.c) aswell as in the corresponding TLC file (see example given at https://de.mathworks.com/help/rtw/tlc/wrapping-user-code-with-tlc.html ).
@MATLAB Support: It´s a shame, that this fix is not mentioned in any of the old example documentation pages. Also you don´t find this subpage easily if you google the error message. Please fix that!
  1 Comment
johnnynoc4sh
johnnynoc4sh on 5 May 2022
Edited: johnnynoc4sh on 12 May 2022
Addtional information: Had the same error again with 2021b. Now this helped me out:
Add the external *.c and header *.h file to the config pane (in my case cJSON.c and cJSON.h).

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink Coder in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!