Clear Filters
Clear Filters

Unresolved externals when creating a mex function

2 views (last 30 days)
Doron Joffe
Doron Joffe on 3 May 2024
Edited: Divyesh on 3 Jul 2024 at 10:57
I am creating a simple mex function for the OpenCV imread() function. This will allow me to read freames from a camera stream.
I receive an error stating that I have two, unresolved externals as shown below:
error LNK2019: unresolved external symbol "class cv::Mat __cdecl cv::imread(class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > const &,int)"
I am able to use the imread() functionality directly from Visual Studio in my Python and C++ scripts so there is no problem with my OpenCV download. In the code that I have pasted below, all functionallity with the cv::createBackgroundSubtractorMOG2() works perfectly. ie: when I compile the mex function with only that functionallity, it works fine.
Why is it that functionallity with the cv::createBackgroundSubtractorMOG2() works fine but when I add in the imread() function it gives me the errors.
This is my code:
#include "opencvmex.hpp"
using namespace cv;
static Ptr<BackgroundSubtractorMOG2> ptrBackgroundModel = cv::createBackgroundSubtractorMOG2();
// Static smart pointer to hold the loaded image
static Ptr<Mat> ptrImage = makePtr<Mat>();
// Function to load an image from a file
void loadImageFromFile(const std::string& filename) {
// Load the image from file
Mat image = cv::imread(filename);
// Assign the loaded image to the static smart pointer
*ptrImage = image;
James Tursa
James Tursa on 3 May 2024
Edited: James Tursa on 3 May 2024
Why is the error message showing a imread() signature with four arguments but your example code imread() has only one argument? Are there other imread() calls in your code that have four arguments?
Are there multiple libraries you need to link with?
Doron Joffe
Doron Joffe on 10 May 2024
I managed to resolve the problem. The issue was that I was not linking the opencv lib file in the mex call. When compiling my mex file, the call needed to be mexOpenCV loadImage.cpp -lopencv_world460.lib

Sign in to comment.

Answers (1)

Divyesh on 16 May 2024
Edited: Divyesh on 3 Jul 2024 at 10:57
The error "unresolved external symbol" for cv::imread indicates your MEX function can't find the implementation for that function when compiling. Here's why this might be happening even though you can use imread directly in your scripts:
Reason: Linking Issue
  • Your MEX function compiles fine but fails to link with the OpenCV library where imread is defined.
Possible Solutions:
  1. Link with OpenCV Libraries:
  • During MEX function compilation, you need to specify the OpenCV libraries to link against. The exact steps might differ depending on your development environment (e.g., Visual Studio, MATLAB). However, you'll generally need to provide the path to the OpenCV library files (.lib files) during the compilation process.
  1. MEX Configuration Options:
  • Some MEX configuration tools might have options to automatically link with commonly used libraries like OpenCV. Explore your specific MEX configuration settings and see if there's an option to include OpenCV libraries.
  1. Pre-compiled Headers:
  • If you're using pre-compiled headers in your project, ensure they include the necessary OpenCV headers. Make sure the header that defines cv::imread is included in the pre-compiled header.
Here are some additional tips:
  • Consult the documentation for your specific MEX compiler/development environment for detailed instructions on linking external libraries.
  • Double-check the paths you're providing for the OpenCV libraries during compilation.
  • Search online forums and communities for similar issues related to linking OpenCV with MEX functions. There might be specific solutions for your development environment.
By addressing the linking issue, you should be able to resolve the unresolved external symbol error for cv::imread in your MEX function. I have recently met a healthcare software development and it was written good, i learnd how to articluate from there. You can also visit Telemedicine app development to know more about it

Community Treasure Hunt

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

Start Hunting!