Clear Filters
Clear Filters

How to include a DLL when building a Simulink model including S-Functions?

7 views (last 30 days)
I need to build a Simulink model so that I can run it in External Mode. In my model, I have used S-Function blocks that call C/C++ functions to access a device (US Digital USB4). The vendor of the device has provided C functions in a DLL, and I have called those functions in my own C code, and used Legacy Code Tool to integrate it into Simulink. When running in Normal mode, everything is okay. But when I try to build the model, I get error LNK2019 (unresolved external symbol error):
### Linking ...
"D:\MATLAB\R2014a\sys\perl\win32\bin\perl" "D:\MATLAB\R2014a\rtw\c\tools\mkvc_lnk.pl" untitled.lk untitled.obj USB4init.obj untitled_data.obj untitled_main.obj rt_sim.obj rtiostream_utils.obj
link /DEBUG /DEBUGTYPE:cv /INCREMENTAL:NO /NOLOGO -subsystem:console,5.02 /NODEFAULTLIB:libc.lib /NODEFAULTLIB:libcmt.lib /NODEFAULTLIB:msvcrtd.lib /NODEFAULTLIB:libcd.lib /NODEFAULTLIB:libcmtd.lib /NODEFAULTLIB:msvcprtd.lib extmode_quarc_r2013b.lib quanser_communications.lib kernel32.lib ws2_32.lib mswsock.lib advapi32.lib msvcprt.lib winmm.lib /MANIFESTFILE:untitled.manifest @untitled.lk @untitled_ref.rsp quanser_runtime.lib quanser_common.lib shell32.lib -out:..\untitled.rt-win64
USB4init.obj : error LNK2019: unresolved external symbol USB4_Initialize referenced in function "int __cdecl USB4init(short)" (?USB4init@@YAHF@Z)
which means that the compiler cannot find the function USB4_Initialize() which is one of the functions provided by the vendor in the DLL file. I have #included the associated header (USB4.h) file in my own function (USB4init), and all of the necessary files (USB4.h, USB4.lib, USB4.dll) are inside the MATLAB directory. My MATLAB is R2014a (64 bit) with Visual Studio 2012 C++ Compiler.
Additional information: my C function header file (USB4init.h):
int USB4init(short iDeviceCount);
my C function body (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 these are the commands I'm using to create the Simulink model with Legacy Code Tool:
def = legacy_code('initialize');
def.Options.language = 'C++';
def.SourceFiles = {'USB4init.cpp'};
def.HeaderFiles = {'USB4init.h','USB4.h'};
def.SFunctionName = 'USB4initialize';
def.OutputFcnSpec = 'int16 y1 = USB4init(int8 u1)';
def.HostLibFiles = {'USB4.lib'};
legacy_code('slblock_generate', def);
legacy_code('sfcn_tlc_generate', def);
legacy_code('sfcn_cmex_generate',def);
legacy_code('compile',def);

Answers (1)

Bara Emran
Bara Emran on 10 Nov 2015
Hi Alireza, I am trying to work with USDigital USB4. Could you share more information about you project, what was the result and if it possible to share a simulink file ?

Categories

Find more on Simulink Coder 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!