Writing S-Functions    

Source File Format

The format of the C++ source for an S-function is nearly identical to that of the source for an S-function written in C. The main difference is that you must tell the C++ compiler to use C calling conventions when compiling the callback methods. This is necessary because the Simulink simulation engine assumes that callback methods obey C calling conventions.

To tell the compiler to use C calling conventions when compiling the callback methods, wrap the C++ source for the S-function callback methods in an extern "C" statement. The C++ version of the sfun_counter S-function example (matlabroot/simulink/src/sfun_counter_cpp.cpp) illustrates usage of the extern "C" directive to ensure that the compiler generates Simulink-compatible callback methods:


  Creating C++ S-Functions Making C++ Objects Persistent