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.expex_sfun_USB4init.obj : error LNK2019: unresolved external symbol USB4init referenced in function mdlOutputsUSB4init.obj : error LNK2019: unresolved external symbol USB4_Initialize referenced in function "int __cdeclUSB4init(short)" (?USB4init@@YAHF@Z)ex_sfun_USB4init.mexw64 : fatal error LNK1120: 2 unresolved externalsError 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!
Best Answer