1. Test environment:
- Operating system: Windows ten, 64bit
- Compiler: MINGW64
- 32-bit system please download the corresponding 32-bit version
- matlab2017a
- It will be slightly different after 2017a
2. Configure the environment and process:
Download compiler MinGW64, for 64-bit systems, you need to download an installer, you can install the corresponding version of the GCC compiler, I choose gcc-4.9.4, in the test environment can run properly
- Download down the installer is mingw-w64-install, installation attention to choose 64-bit, while the installation is finished there is a hole: space .
- When you change the default installation path of the downloader "C:\Program files\mingw-w64\x86_64-4.9.4-win32-seh-rt_v5-rev0\mingw64" contains spaces, MATLAB does not accurately identify So you can move the mingw64 to a path without spaces, such as "C:\mingw64".
Verify that:
Enter the following as shown:
This way the MinGW compiler is configured.
3. Quick compile:
setenv(‘MW_MINGW64_LOC‘,‘C:\mingw64‘)myFunctionName = ‘SFunctionName‘def = legacy_code(‘initialize‘);def.SFunctionName = myFunctionName ;% 注意,输入和输出的变量的名称只能是u1,u2...和y1,y2...def.OutputFcnSpec = ‘double y1 = add_2(double u1, double u2)‘;% 设置用到的头文件或者源文件def.HeaderFiles = {‘add_2.h‘};def.SourceFiles = {‘add_2.c}def.InitializeConditionsFcnSpec = ‘buffer_init()‘;legacy_code(‘sfcn_cmex_generate‘, def);legacy_code(‘compile‘, def) ;
The above script can be quickly compiled in MATLAB, and generate the files available in Simulink, add the S-function module in Simulink, and set the name to "Add_2".
ADD_2.C can directly write a two number of the program to verify the addition of a bit; Add_2.h write a function declaration is also possible.
// add_2.c#include "add_2.h"double add_2(double a, double b){ return a + b ;}// add_2.h#ifndef ADD_2_H#define ADD_2_Hdouble add_2(double a, double b);#endif
4. Reference
- Mathwors a description of the space
- matlab2017b and how to set it afterwards
- Thanks for the video commentary of India's little brother
Sfunction some pits in C in the Matlab of Windows