How to Create a Standalone .exe File from Simulink Code?
21 次查看(过去 30 天)
显示 更早的评论
Hello Community,
I'm currently working on creating a standalone executable (.exe) from my Simulink project, and I need some guidance.
Here’s my current setup:
- I have a .m file that loads variables into the workspace and runs the simulation. It also processes and visualizes the results afterward.
- I used Simulink Coder to generate C code from the Simulink model. I chose the option to generate code only (without "Execute Makefile"), so now I have a collection of .c and .h files.
My questions:
- How can I use these generated files to build a standalone .exe file?
- Do I need additional files from the MATLAB/Simulink installation (e.g., libraries, .c files or other runtime components)?
- Is it possible to use Visual Studio or another compiler to compile the .c files into an executable? If yes, are there specific steps or configurations I need to follow?
I'm relatively new to this process, so I appreciate any instructions or resources to help me bridge the gap from generated C code to a functional executable.
Thank you in advance for your help!
Best regards,
Aaron
2 个评论
Cris LaPierre
2025-1-27
Here's an answer from the MathWorks Support Team: https://www.mathworks.com/matlabcentral/answers/100016-how-can-i-create-an-executable-from-my-simulink-model-to-send-to-someone-who-does-not-have-matlab
Andreas Goser
2025-1-28
I am under the impression the user needs one additional step like described here https://www.mathworks.com/help/slcompiler/ug/deploy-a-simulation-with-simulink-compiler.html
回答(1 个)
Pramil
2025-2-6
Hi Aaron,
I had a similar usecase where I needed to make some changes to the generated code from a Simulink Model and later create an EXE for the same outside of MATLAB. This method though requires that you have a C compiler and Cmake installed in your system.
For the above I followed these steps:
- Created a "packNGo" version of my generated model. Refer to the following link to know more about this function: https://www.mathworks.com/help/releases/R2024b/coder/ref/packngo.html
- To create "packNGo" version just load the "buildInfo" MAT file in MATLAB present inside the generated code folder.
- Use the following command to create a "flat" pack type folder containing all the necessary files require to create EXE file outside of MATLAB.
packNGo(buildInfo,'packType','flat','filename','your_filename');
- Once you have this folder, you can use this to create an EXE file in VS Code or Visual Studio by using a "CMakeLists.txt" file like below:
cmake_minimum_required(VERSION 3.10)
# Set the project name and version
project(temp VERSION 1.0)
# Specify the C standard
set(CMAKE_C_STANDARD 99)
set(SOURCES_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
# Use file(GLOB ...) to collect all .c files in the sources directory
file(GLOB SOURCES "${SOURCES_DIR}/*.c")
# Use file(GLOB ...) to collect all .h files in the sources directory
file(GLOB HEADERS "${SOURCES_DIR}/*.h")
# Add the executable target
add_executable(temp ${SOURCES} ${HEADERS})
target_compile_definitions(temp PRIVATE MODEL=temp NUMST=5 ONESTEPFCN=1 TERMFCN=1)
- Add this file in the same folder and create a build folder at the same path, open a terminal in the build folder and run the following commands:
1. cmake ..
2. cmake --build .
- This would create an EXE file with name "temp.exe" you can change the same as per your requirements in the "CMakeLists.txt" file, as well as the model name(MODEL), sample time(NUMST) etc.
Hope this helps.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!