Error LNK2019 (unresolved external symbol) when trying to integrate C function using Legacy Code Tool
25 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Alireza Manzoori
el 12 de Ag. de 2015
Comentada: Salah
el 27 de Dic. de 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 comentarios
Walter Roberson
el 13 de Ag. de 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.
Respuesta aceptada
Alireza Manzoori
el 13 de Ag. de 2015
4 comentarios
Jorg Entzinger
el 24 de Jul. de 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
el 27 de Dic. de 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++.
Más respuestas (0)
Ver también
Categorías
Más información sobre MATLAB Support for MinGW-w64 C/C++ Compiler en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!