Build (linker) error by building C++ function with third-party library (OpenCV) in Simulink C/C++ Code Block
14 views (last 30 days)
Show older comments
Dear community,
I want to add custom C++ code in a Simulink s-function, which is working well outside of MATLAB/Simulink. Unfortunately I am stuck since days, because of a build error with error messages as below:
### 'SemSegONNX.cpp' created successfully
### 'SemSegONNX_wrapper.cpp' created successfully
C:\Users\usr\AppData\Local\Temp\mex_89125236923555_19680\tpf5dfed4c_669f_4bd9_a2e8_0609ae30efc6.obj:tpf5dfed4c_669f_4bd9_a2e8_0609ae30efc6.cpp:(.text+0x9b): undefined reference to `cv::Mat::zeros(cv::Size_, int)'
C:\Users\usr\AppData\Local\Temp\mex_89125236923555_19680\tpf5dfed4c_669f_4bd9_a2e8_0609ae30efc6.obj:tpf5dfed4c_669f_4bd9_a2e8_0609ae30efc6.cpp:(.text+0xc6): undefined reference to `cv::Mat::Mat()'
C:\Users\usr\AppData\Local\Temp\mex_89125236923555_19680\tpf5dfed4c_669f_4bd9_a2e8_0609ae30efc6.obj:tpf5dfed4c_669f_4bd9_a2e8_0609ae30efc6.cpp:(.text+0x126): undefined reference to `cv::Mat::~Mat()'
C:\Users\usr\AppData\Local\Temp\mex_89125236923555_19680\tpf5dfed4c_669f_4bd9_a2e8_0609ae30efc6.obj:tpf5dfed4c_669f_4bd9_a2e8_0609ae30efc6.cpp:(.text+0x12f): undefined reference to `cv::Mat::~Mat()'
C:\Users\usr\AppData\Local\Temp\mex_89125236923555_19680\tpf5dfed4c_669f_4bd9_a2e8_0609ae30efc6.obj:tpf5dfed4c_669f_4bd9_a2e8_0609ae30efc6.cpp:(.text+0x138): undefined reference to `cv::Mat::~Mat()'
C:\Users\usr\AppData\Local\Temp\mex_89125236923555_19680\tpf5dfed4c_669f_4bd9_a2e8_0609ae30efc6.obj:tpf5dfed4c_669f_4bd9_a2e8_0609ae30efc6.cpp:(.text+0x1fd): undefined reference to `cv::Mat::~Mat()'
C:\Users\usr\AppData\Local\Temp\mex_89125236923555_19680\tpf5dfed4c_669f_4bd9_a2e8_0609ae30efc6.obj:tpf5dfed4c_669f_4bd9_a2e8_0609ae30efc6.cpp:(.text+0x219): undefined reference to `cv::Mat::~Mat()'
C:\Users\usr\AppData\Local\Temp\mex_89125236923555_19680\tpf5dfed4c_669f_4bd9_a2e8_0609ae30efc6.obj:tpf5dfed4c_669f_4bd9_a2e8_0609ae30efc6.cpp:(.text+0x225): more undefined references to `cv::Mat::~Mat()' follow collect2.exe: error: ld returned 1 exit status
I add the following C++ code in a "C/C++ Code Block" which should build:
#include <opencv2/opencv.hpp>
#include <vector>
void SemSegONNX_Start_wrapper(void)
{
}
void SemSegONNX_Outputs_wrapper(const uint8_T *img, uint8_T *pred)
{
// generate empty dummy image with shape 224x224
cv::Mat image = cv::Mat::zeros(cv::Size(224,224),CV_8UC1);
// only to generate vectorized outputs from image
for (short i = 0; i < 224; ++i)
for (short j = 0; j < 224; ++j)
pred[i*224 + j] = img[i, j];
}
void SemSegONNX_Terminate_wrapper(void)
{
}
The shown code shows only a minimal example of the code I want to run, but the occurring errors are the same. It seems that there is a general problem. The compilation process runs well, but the linker cannot find the included library. Via the command line, I can compile the code successfully by:
g++ -Wall -o _foo.exe -Ipackages\onnxruntime-win-x64-1.12.0\include -ID:\opencv\build_mingw_x64\install\include -Lpackages\onnxruntime-win-x64-1.12.0\lib -LD:\opencv\build_mingw_x64\install\x64\mingw\bin _foo.cpp -lopencv_world470 -lonnxruntime
My understanding of the documentation of the generation of the mex-function is that that is only a wrapper build around the custom C++ code. So I am afraid the problem is a correct setup of the LIB_PATH, INC_PATH and ENTRY. The way I have done this as follows seems to be not correct.
LIB_PATH D:\opencv\build_mingw_x64\install\x64\mingw\bin
INC_PATH D:\opencv\build_mingw_x64\install\include
ENTRY libopencv_world470.dll
All shown steps are done on a Windows 10, MinGW-W64 gcc 13.0.1, MATLAB Support for MinGW-w64 C/C++ Compiler v22.2.0
I am happy to get support in this field. Thanks in advance.
Best regards
0 Comments
Answers (1)
Rasmita
on 16 Mar 2023
Hi,
It is my understanding that, you are getting above mentioned build error when you add the custom C++ code that uses OpenCV in a Simulink s-function.
The error
undefined reference to `cv::Mat::__()'
may be due to some missing OpenCV libraries. Please search in the official website of OpenCV to get those libraries and link it.
Hope this helps!
Regards,
Rasmita
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!