Build (linker) error by building C++ function with third-party library (OpenCV) in Simulink C/C++ Code Block

13 次查看(过去 30 天)
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

回答(1 个)

Rasmita
Rasmita 2023-3-16
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
  1 个评论
greencactus
greencactus 2023-3-31
Hello Rasmita,
Thanks for your answer. As described, the code is successfully compilable and executable outside of MATLAB. The library OpenCV is pre-compiled for my operating system and available via the set paths:
LIB_PATH D:\opencv\build_mingw_x64\install\x64\mingw\bin
INC_PATH D:\opencv\build_mingw_x64\install\include
ENTRY libopencv_world470.dll
Via the command line, I can compile the code with linker arguments:
g++ -Wall -o _foo.exe -ID:\opencv\build_mingw_x64\install\include -LD:\opencv\build_mingw_x64\install\x64\mingw\bin _foo.cpp -lopencv_world470
Only the linker of MATLAB's C++ s-function block cannot find the linked libraries. This behavior raises the error. Further, the file opencv_world470.dll is a dynamic library. It did not work with static *.lib files, either. I have the strong suspicion that some configuration of the C++ s-function block is wrong.
The main idea of the integration of C++ code is to refactor code from other projects. I know about the OpenCV interface from MATLAB. I do not need OpenCV at all, but it is representative for other third party C++ libraries. It would be very helpful if you could provide a minimal example of this block with included third party libraries.
Best regards

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by