How do I compile S-functions from within Microsoft Visual Studio environment?

23 views (last 30 days)
I want to compile an S-function from within Visual Studio using the "build" command in the menus instead of using "mex" from either the MATLAB or DOS command line.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 4 Nov 2016
To create a Visual Studio solution (.sln) file that will build a ".mex" file for use in MATLAB/Simulink based on existing source code, please do the following:
1) Open Visual Studio, and create a new project by selecting "File"->"New"->"Project from existing code"
2) Choose "Visual C++" as the type of project.
3) You may choose any project location that you like, but the project name should match the name of the resulting S-function. That means that the project is intended to produce <project name>.mexw<32|64>, and that the source code includes the preprocessor directive: "#define S_FUNCTION_NAME <project name>"
4) Using the "Add" button, select all of the user-defined source files and add them to the project.
5) In the next pane, select "Use Visual Studio" as the method for building the project, and for the "Project Type" select "Dynamically Linked Library (DLL) Project". Click 'Next'.
6) For the "Preprocessor definitions" field add
MATLAB_MEX_FILE
7) For the "Include Search Paths" field, add
"<MATLABROOT>\simulink\include"
and
"<MATLABROOT>\extern\include"
(including quotes) where <MATLABROOT> is the absolute path to the MATLAB installation.
8) Finish creating the project
9) Right-click on the newly-created project in the Solution Explorer and select "Properties" and make the following changes in the dialog that appears:
10) Click the "Configuration Manager" button, and in the dialog that appears open the "Active solution platform" drop-down menu. If you want to create an S-Function for 64-bit MATLAB, make sure "x64" is selected. If it is available, select it. If not, select "<new...>" and select "x64" from the dialog that appears. If the "Platform" for your project in the subwindow below is "Win32" change it to "x64" as well. If "x64" is not an option, create it by selecting "<new>". When the new window opens, select "x64" under "New platform" and unclick "Create new solution platform". Click "OK" and "Close" to apply these changes to the Property window.
11) In the "Linker" pane, select the "General" section, and edit the file extension in the "Output File" section, changing it from ".dll" to ".mexw32" or ".mexw64". (This is why the project name has to match the S-function name, ensuring that the generated ".mex" file name matches the "S_FUNCTION_NAME" defined in the source code.)
12) In the "Linker" pane, select the "Input" section and add to "Additional dependencies" section, and add the following(including quotes, and expanding <MATLABROOT> to be the MATLAB installation directory):
"<MATLABROOT>\extern\lib\win64\microsoft\libmx.lib"
"<MATLABROOT>\extern\lib\win64\microsoft\libmat.lib"
"<MATLABROOT>\extern\lib\win64\microsoft\libmex.lib"
(If you are creating an S-Function for 32-bit MATLAB, please replace win64 by win32)
13) In the "General" pane, set "Platform Toolset" to "Windows7.1SDK".
14) Click "Apply" and close the dialog box.
15) Select "Project"->"Add"->"New Item".
16) If you have a "Module-Definition file" option, select "Module-Definition file" from the GUI, give the file a name, and then click "Add".
16a) Edit the newly-created module-definition file to be the following, where <s-function name> is the project/S-function name, and <extension> is the appropriate extension for the target architecture (e.g. "mexw32" or "mexw64"):
LIBRARY"<s-function name>.<mex extension>"
EXPORTS
mexFunction
17) If you do not have a "Module-Definition file" option, you would need to create this file outside of Visual Studio and import it. Create a text file with the following information inside it:
LIBRARY"<s-function name>.<mex extension>"
EXPORTS
mexFunction
Save the file with a name to the folder that contains the Visual Studio Solution.
17a) Change the extension from ".txt" to ".def". Inside Visual Studio, select "Project"->"Add"->"Existing Item" and select the .DEF file.
At this point, you should be able to select the "build" option from the menus to create a ".mex" file that will work with Simulink.
Please note: For distributing S-functions created this way, it is necessary to use the "release" output from Visual Studio, not the "debug" output. For each mode selected you would need to make sure the steps starting from step 9 are set in that mode. In some cases, the "debug" output will require debuggable versions of standard DLLs, which could clash with normal versions already loaded by Simulink.
Attached is a ZIP file that contains the an example Visual Studio Solution and files configured for building S-function MEX according to the instructions above. The ZIP file contains the following:
1) "subsystem_sfcn_rtw" folder containing the C and H files for the S-function
2) "Project" folder containing the Visual Studio files.

More Answers (0)

Categories

Find more on C Shared Library Integration in Help Center and File Exchange

Products


Release

R2012b

Community Treasure Hunt

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

Start Hunting!