MATLAB: Do I get a Linker error when using ‘xpce*’ functions in the own xPC-UDP-block with xPC Target 4.1 (R2009a)

linkers-functionSimulink Real-Timetargetudpxpcxpcexpceudpopensendxpceudpsend

I need to modify the shipped xPC udp block to fit my needs. So, I created a C++ S-function block that closely resembles the shipped UDP block in xPC Target 4.1 (R2009a). I am using xpce* functions declared in 'xpcimports.h' in my S-function code. For compiling I am using the following command with MSVC++ 2008 (Version 9.0) compiler:
mex -L"<MSVC INSTALLATION FOLDER>\Microsoft Visual Studio 8\VC\PlatformSDK\Lib" -lWS2_32 MTX_UDP_Send_v2.cpp
The code successfully compiles and I could simulate the model.
However, when building the model for xPC Target, I am getting the following Linker error:
MTX_UDP_Send_v2.obj : error LNK2019: unresolved external symbol "int (__cdecl* xpceUDPOpenSend)(char const *,int,int,int,int)" (?xpceUDPOpenSend@@3P6AHPBDHHHH@ZA) referenced in function "void __cdecl mdlStart(struct SimStruct_tag *)" (?mdlStart@@YAXPAUSimStruct_tag@@@Z)
MTX_UDP_Send_v2.obj : error LNK2019: unresolved external symbol "unsigned char (__cdecl* xpcIsModelInit)(void)" (?xpcIsModelInit@@3P6AEXZA) referenced in function "void __cdecl mdlStart(struct SimStruct_tag *)" (?mdlStart@@YAXPAUSimStruct_tag@@@Z)
MTX_UDP_Send_v2.obj : error LNK2019: unresolved external symbol "int (__cdecl* xpceUDPSend)(int,unsigned char *,int)" (?xpceUDPSend@@3P6AHHPAEH@ZA) referenced in function "void __cdecl mdlOutputs(struct SimStruct_tag *,int)" (?mdlOutputs@@YAXPAUSimStruct_tag@@H@Z)
MTX_UDP_Send_v2.obj : error LNK2019: unresolved external symbol "void (__cdecl* xpceUDPClose)(int)" (?xpceUDPClose@@3P6AXH@ZA) referenced in function "void __cdecl mdlTerminate(struct SimStruct_tag *)" (?mdlTerminate@@YAXPAUSimStruct_tag@@@Z)
Is there an issue with my code?

Best Answer

The issue is due to the fact that C++ compilers mangles the names of functions declared in the 'xpcimports.h' file. Since the 'xpce*' functions defined in this C file is mangled by C++ compiler, linker could not find these functions during linking stage of compilation.
To workaround this issue, you can use either one of the following methods:
1) You can surround the code that imports the 'xpcimports.h' file with extern "C" command as shown below:
extern "C" {
#include "xpcimports.h"
}
2) Convert the CPP S-function that you have to a C S-function.