Getting "LNK2019 unresolved external symbol _ ..." errors when compiling C++ program linked to MATLAB libraries

11 views (last 30 days)
I'm writing a C++ program to read in data files with code I already have and ouput them to *.mat files. The program uses the following:
from libmx.lib
* mxlsFromGlobalWS
* mxGetString
* mxGetPr
* mxGetNumberOfDimensions
* mxDestoryArray
* mxCreateString
* mxCreateDoubleMatrix
from libmat.lib
* matPutVariable
* matPutVariableAsGlobal
* matOpen
* matGetVariable
* matClose
I went to properties > Linker > General and put into 'Additonal Library Directories' and added:
C:\Program Files\\MATLAB\\R2019a_NMCI\\extern\\lib\\win64\\microsoft
I went to properties > Linker > Input and put into 'Additional Dependencies' and added:
libmx.lib;libmat.lib
Visual Studio made it into this, but I think it's the same thing.
libmx.lib;libmat.lib;%(AdditionalDependencies)
And yet, I am still getting the dreaded LNK2019 Error for the abover functions from the libmat and libmx libraries. What else should be done???

Answers (2)

Aishwarya Shukla
Aishwarya Shukla on 31 Mar 2023
The LNK2019 error indicates that the linker is unable to find the definitions for the functions you are trying to use from the libmat and libmx libraries. This error can occur if the libraries are not linked properly or if the libraries are not located in the correct directories.
Here are some additional steps you can try:
  1. Double-check that the library directories and library names are spelled correctly in the Visual Studio project properties. Make sure there are no typos or extra spaces.
  2. Check that the libraries are in the directories you specified in the project properties. If they are not, you may need to download and install the MATLAB Runtime or MATLAB Compiler Runtime libraries.
  3. Try adding the MATLAB library directories to your system's PATH environment variable. To do this, go to Control Panel > System > Advanced System Settings > Environment Variables, and add the MATLAB library directories to the PATH variable.
  4. Make sure you are using the correct version of the MATLAB libraries. If you are using a different version of MATLAB than R2019a, you may need to update your libraries or use the correct version of MATLAB.
  5. Check that the functions you are using from the libmat and libmx libraries are actually available in those libraries. Sometimes, different versions of the libraries may have different function names or may not have certain functions at all.
  6. Try including the header files for the libmat and libmx libraries in your C++ code. This can help ensure that the function definitions are properly linked with your code.
I hope these suggestions help you resolve the LNK2019 error and successfully link your program with the libmat and libmx libraries.
  1 Comment
Anne Davenport
Anne Davenport on 2 May 2023
Well, it seems a shame to leave this hanging, so I'll first say that I did try all 6 things on list and none of them worked. I still got the LNK2019 error. The problem always seemed to boil down to no being able to get MS Visual Studio to understand where that library file. I talked to someone who had dealt with this kind of error and his only response was something like, "Oh, those are really hard to find."
How did I solve the problem? I didn't. I went around it. Instead of using C code (that I already had) to read the text files and then produce a *.mat file (which is what I needed the library for), I used MATLAB code to read the text file and product the *.mat file, which is trivial. It turns out I already had the MATLAB code to read some of the text files, so it wasn't as much extra work as I thought it would be.
Not a really satisfying answer, but it worked.

Sign in to comment.


Anne Davenport
Anne Davenport on 4 Apr 2023
Thanks for the reply, but I still have the LNK2019 error:
(1) Double-check that the library directories and library names are spelled correctly in the Visual Studio project properties. Make sure there are no typos or extra spaces.
I checked. The library names look right. No typos
(2) Check that the libraries are in the directories you specified in the project properties. If they are not, you may need to download and install the MATLAB Runtime or MATLAB Compiler Runtime libraries.
Yes, the libraries are in this directory --- C:\Program Files\MATLAB\R2019a_NMCI\extern\lib\win64\microsoft
I even tried copying the library files into a 'lib' directory with the project and linking to that, but that did not work either.
(3) Try adding the MATLAB library directories to your system's PATH environment variable. To do this, go to Control Panel > System > Advanced System Settings > Environment Variables, and add the MATLAB library directories to the PATH variable.
Haven't tried this -- I don't have access to Adavance System Settings on my network and I'd really hate to get my IT department involved in what looks like a Visual Studio problem.
(4) Make sure you are using the correct version of the MATLAB libraries. If you are using a different version of MATLAB than R2019a, you may need to update your libraries or use the correct version of MATLAB.
Yes, they are the correct library files for VS 2019a.
(5) Check that the functions you are using from the libmat and libmx libraries are actually available in those libraries. Sometimes, different versions of the libraries may have different function names or may not have certain functions at all.
Don't know how to do that. I tried 'dumpbin' in a Visual Studio window, but my VS does not seem to know what 'dumpbin' is. This seems unlikely and I'm hoping to not chase down a new 'dumpbin' rabbit-hole for the orginal link problem.
(6) Try including the header files for the libmat and libmx libraries in your C++ code. This can help ensure that the function definitions are properly linked with your code.
Yes, the header files are included in the main program:
#include "mat.h" #include "matrix.h"
The functions that are not linking are in these header files.
So, what I'm doing to try to link to these files is:
Properties >> VC++ >> Diretories >> Library Directories (edit)
C:\Program Files\MATLAB\R2019a_NMCI\extern\lib\win64\microsoft (this is where the lib files are)
Properties >> Linker >> Additional Dependencies (edit)
libmx.lib, libmat.lib
But I still get unresolved externals, LNK2019. Any other suggestions or has anyone else had this problem? Thanks for looking at this.
Anne D.

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!