Error LNK2019 (unresolved external symbol) when trying to integrate C function using Legacy Code Tool

13 visualizaciones (últimos 30 días)
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

Iniciar sesión para comentar.

Respuesta aceptada

Alireza Manzoori
Alireza Manzoori el 13 de Ag. de 2015
I finally managed to solve the problem...So for further reference, I'll explain my solution.
There were two problems, in fact. The first problem was that, as Walter suggested in his comments above, the DLL file that I was using was the 32-bit version but my MATLAB is 64-bit, so I used the 64-bit DLL instead. I owe a big thank you to Walter Robertson for his useful comment!
So this alleviated one of the linking errors, namely, this one:
error LNK2019: unresolved external symbol USB4_Initialize referenced in function "int __cdecl USB4init(short)" (?USB4init@@YAHF@Z)
But this one was still there:
ex_sfun_USB4init.obj : error LNK2019: unresolved external symbol USB4init referenced in function mdlOutputs
Then I came across this thread, where someone with an almost similar problem had it resolved by changing the compiler language from C to C++. So this gave me the idea that maybe changing the format of the S-Function created by the Legacy Code Tool from *.c to *.cpp and compiling the C++ S-Function instead might work for me. So I went to my Matlab directory, renamed "ex_sfun_USB4init.c" to "ex_sfun_USB4init.cpp", then used this command:
mex('ex_sfun_USB4init.cpp', '-IC:\Users\Alireza\Documents\MATLAB', 'C:\Users\Alireza\Documents\MATLAB\USB4.lib', 'C:\Users\Alireza\AppData\Local\Temp\tpf230d37c_7170_4b16_b10a_398e3a794cac\USB4init.obj')
...and voila, problem solved! :)
  4 comentarios
Jorg Entzinger
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
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++.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Simulink en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by