MATLAB Answers

Tim
1

error LNK2019: unresolved external symbol when compiling MEX

Asked by Tim
on 21 Nov 2013
Latest activity Commented on by hassan mahmood on 20 May 2014
I'm trying to compile some files into MEX and I get a linkage error.
I have:
  • helloworld.cpp which includes gateway function
  • ftdi.h class declaration
  • ftdi.cpp a source file
  • FTD2XX.h and FTD2XX.lib a library and a header
All files are in the same folder and this folder is my current working directory in MATLAB.
I'm using MS VS2010 and MATLAB R2013a on a 64bit Windows 7. I set up the compiler using mex -setup and was able to create and run some test MEX files.
As I understand, I need to compile both helloworld.cpp and fdti.cpp when I run
mex helloworld.cpp fdti.cpp
I get:
ftdi.obj : error LNK2019: unresolved external symbol __imp_FT_ListDevices referenced in function "public: int __cdecl ftdi::GetDeviceCount(void)" (?GetDeviceCount@ftdi@@QEAAHXZ)
ftdi.obj : error LNK2019: unresolved external symbol __imp_FT_Open referenced in function "public: int __cdecl ftdi::OpenDevice(int)" (?OpenDevice@ftdi@@QEAAHH@Z)
and more of similar errors.
ftdi::GetDeviceCount(void)
and
ftdi::OpenDevice(int)
are functions in ftdi.cpp
I also tried:
mex helloworld.cpp
and got similar errors. When I tried:
mex -IC:\Users\tnosov\Documents\MATLAB\newtest20nov helloworld.cpp
this is the path to where all my files are located and I got the same errors.
I've been stuck with this problem for many days now, I tried many other things I found here and in other places on the internet, but nothing helps. I'll be thankful for any suggestions and comments.
Thank you!
Tim
here's helloworld.cpp:
#include <mex.h>
//for FTDI
#include "ftdi.h"
ftdi usb; //initialize an instance of class ftdi
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[
// {
// mexPrintf("bye World!\n");
// cout << endl << "Error initializing device";
// }
// else
// {])
{
mexPrintf("Hello Worl
// }
d!\n");
}

  1 Comment

very help full. i encountered this problem using pthread library. thanks :)

Sign in to comment.

3 Answers

Answer by Tim
on 27 Nov 2013
 Accepted Answer

So here's my solution and explanation:
The error I got:
error LNK2019: unresolved external symbol __imp_FT_ListDevices referenced in function "public: int __cdecl ftdi::GetDeviceCount(void)" (?GetDeviceCount@ftdi@@QEAAHXZ)
This means that in the function ftdi::GetDeviceCount(void) the program couldn't understand what FT_ListDevices means. The declaration and definition of FT_ListDevices was in a library called ftd2xx. What I was trying to do initially, when I was trying to compile MEX file I was trying to link my program with ftd2xx.dll. However, for MEX files you need to link them with lib library, ftd2xx.lib. So I found and downloaded lib version of the library and run this command:
mex -lFTD2XX -L"C:\Users\tnosov\Downloads\CDM 2.08.30 WHQL Certified (1)\CDM v2.08.30 WHQL Certified\amd64" control_robot.cpp ftdi.cpp
where:
-lFTD2XX
is the name of the library. Note that this is lower case "L" and there's no space between -l and name of the library. Also, MATLAB adds .lib after whatever you wrote there.
-L"C:\Users\tnosov\Downloads\CDM 2.08.30 WHQL Certified (1)\CDM v2.08.30 WHQL Certified\amd64"
is location of that library. No space after -L and put quotes around path
control_robot.cpp ftdi.cpp
are the files where my code is. In this files I also included header for that library that caused me problems:
#include "C:\Users\tnosov\Dropbox\CO-OP2013\not shared\Code\interfacing_matlab_cpp\CDM 2.08.30 WHQL Certified\CDM v2.08.30 WHQL Certified\ftd2xx.h"
The way I included this header is a pretty horrible programming practice, but it works.
I hope this helps! Let me know if you have run into any kind of similar questions. I spent 2 weeks trying to fix this. I'll be glad to help you!

  2 Comments

very help full. i encountered this problem using pthread library. thanks :)
i have 2 errors to resolve would you look into
http://www.mathworks.com/matlabcentral/answers/130251-linker-error-lnk2005-void-__cdecl-process-void-process-yapeaxpeax-z-already-defined

Sign in to comment.


Answer by James Tursa
on 21 Nov 2013

You mention a library file FDT2XX.lib but I don't see you use it. Do the functions in ftci.cpp depend on this library? If so, try including it in the mex command so it gets linked in (have all files in the current directory):
mex helloworld.cpp fdti.cpp FDT2XX.lib

  1 Comment

Hey James, thanks for your feedback! yes, ftdi.cpp depends on FDT2XX.lib. I just tried what you suggested and it gave me the same errors:
ftdi.obj : error LNK2019: unresolved external symbol __imp_FT_ListDevices referenced in function "public: int __cdecl ftdi::GetDeviceCount(void)" (?GetDeviceCount@ftdi@@QEAAHXZ)
ftdi.obj : error LNK2019: unresolved external symbol __imp_FT_Open referenced in function "public: int __cdecl ftdi::OpenDevice(int)" (?OpenDevice@ftdi@@QEAAHH@Z)
I also tried
mex -lFTD2XX -LC:\Users\tnosov\Documents\MATLAB\newtest20nov helloworld.cpp ftdi.cpp
since mex -help suggests that we add libraries with -l and directories where libraries are with -L, but it gave me the same errors.

Sign in to comment.


Answer by Tim
on 22 Nov 2013

I solved my problem! I will post an update soon. Thanks everyone!

  1 Comment

Sign in to comment.