Error LNK2019 (unresolved external symbol) when trying to integrate C function using Legacy Code Tool
11 views (last 30 days)
Show older comments
Alireza Manzoori
on 12 Aug 2015
Commented: Salah
on 27 Dec 2022
I need to use C functions in a Simulink model using Legacy Code Tool. I'm trying to communicate with some hardware ( US Digital USB4 device), for which the vendor has provided a DLL (USB4.dll) & a C header file (USB4.h) including basic functions. I'm calling these basic functions in my own C function. This is the header file for my function ( "USB4init.h" )
int USB4init(short iDeviceCount);
This is the body of my function ( "USB4init.cpp" ):
#include "USB4.h"
#include "USB4init.h"
int USB4init(short iDeviceCount)
{
int iResult = 0;
iResult = USB4_Initialize(&iDeviceCount); // initialize the card
return iResult;
}
And I'm using these commands in Matlab prompt to make a MEX file from my C function:
def = legacy_code('initialize');
def.SourceFiles = {'USB4init.cpp'};
def.HeaderFiles = {'USB4init.h','USB4.h'};
def.SFunctionName = 'ex_sfun_USB4init';
def.OutputFcnSpec = 'int16 y1 = USB4init(int8 u1)';
legacy_code('sfcn_cmex_generate',def);
legacy_code('compile',def)
But I'm facing this error:
### Start Compiling ex_sfun_USB4init
mex('-IC:\Users\...\MATLAB', '-c', '-outdir', 'C:\Users\...\Temp\tp06a02ca7_d2d8_4315_8e39_cb0a5d511cbf', 'C:\Users\...\MATLAB\USB4init.cpp')
Building with 'Microsoft Visual C++ 2012'.
MEX completed successfully.
mex('ex_sfun_USB4init.c', '-IC:\Users\...\MATLAB', 'C:\Users\...\Temp\tp06a02ca7_d2d8_4315_8e39_cb0a5d511cbf\USB4init.obj')
Building with 'Microsoft Visual C++ 2012 (C)'.
Error using mex
Creating library ex_sfun_USB4init.lib and object ex_sfun_USB4init.exp
ex_sfun_USB4init.obj : error LNK2019: unresolved external symbol USB4init referenced in function mdlOutputs
USB4init.obj : error LNK2019: unresolved external symbol USB4_Initialize referenced in function "int __cdecl
USB4init(short)" (?USB4init@@YAHF@Z)
ex_sfun_USB4init.mexw64 : fatal error LNK1120: 2 unresolved externals
Error in legacycode.LCT/compile (line 368)
Error in legacycode.LCT.legacyCodeImpl (line 84)
Error in legacy_code (line 87)
[varargout{1:nargout}] = legacycode.LCT.legacyCodeImpl(action, varargin{1:end});
All of the dll, header, and cpp files are inside the Matlab directory, and I'm using R2014a version. Any help is much appreciated!
4 Comments
Walter Roberson
on 13 Aug 2015
It depends on which version you downloaded. There are two at http://www.usdigital.com/support/software/usb4-software one of which is 32 bit only.
Accepted Answer
Alireza Manzoori
on 13 Aug 2015
4 Comments
Jorg Entzinger
on 24 Jul 2018
It seems you can make the Legacy Code Tool compile C++ instead of C by setting def.Options.language='C++'; Then you don't have to rename the file and re-run the mex command by yourself.
Salah
on 27 Dec 2022
@Jorg Entzinger Please explain more about this. I'm also facing the same issue but I don't want to change my file extension from C to C++.
More Answers (0)
See Also
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!