Simulink Model S-Function Generation Gives Error with MSVC Compiler in VS cstring file

7 visualizzazioni (ultimi 30 giorni)
Hi
I am using Matlab 2021a
I am getting an error while generating S-function from my simulink model.
I am using Microsoft Visual C++ 2019 (C) for C language compilation.
I created healthy S-functions and their .mex files with this compiler before, but in this Simulink model it gives an error.
While it is creating simModel_sf.c file, in some part it is giving an syntax error in Microsoft Visual Studio 'cstring' file and stops creating .c file and S-function and mex file.
The error message is :
Top model targets built:
Model Action Rebuild Reason
================================================
simModel Failed ForceTopModelBuild was enabled.
0 of 1 models built (0 models already up to date)
Build duration: 0h 8m 34.436s
simModel_sf.c
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\cstring(21): error C2061: syntax error: identifier 'std'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\cstring(21): error C2059: syntax error: ';'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\cstring(21): error C2449: found '{' at file scope (missing function header?)
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\cstring(50): error C2059: syntax error: '}'
C:\Users\Administrator\Desktop\simModelFile\simModel_sfcn_rtw\simModel_sf.c(102309): fatal error C1004: unexpected end-of-file found
and I am copying the cstring file content . The current 'cstring' file content and error creating lines are starting with _STD_BEGIN in 21th line and _STD_END in 50th line.
// cstring standard header (core)
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#pragma once
#ifndef _CSTRING_
#define _CSTRING_
#include <yvals_core.h>
#if _STL_COMPILER_PREPROCESSOR
#include <string.h>
#pragma pack(push, _CRT_PACKING)
#pragma warning(push, _STL_WARNING_LEVEL)
#pragma warning(disable : _STL_DISABLED_WARNINGS)
_STL_DISABLE_CLANG_WARNINGS
#pragma push_macro("new")
#undef new
_STD_BEGIN
#pragma warning(push)
#pragma warning(disable : 4995) // name was marked as #pragma deprecated
using _CSTD size_t;
using _CSTD memchr;
using _CSTD memcmp;
using _CSTD memcpy;
using _CSTD memmove;
using _CSTD memset;
using _CSTD strcat;
using _CSTD strchr;
using _CSTD strcmp;
using _CSTD strcoll;
using _CSTD strcpy;
using _CSTD strcspn;
using _CSTD strerror;
using _CSTD strlen;
using _CSTD strncat;
using _CSTD strncmp;
using _CSTD strncpy;
using _CSTD strpbrk;
using _CSTD strrchr;
using _CSTD strspn;
using _CSTD strstr;
using _CSTD strtok;
using _CSTD strxfrm;
#pragma warning(pop)
_STD_END
#pragma pop_macro("new")
_STL_RESTORE_CLANG_WARNINGS
#pragma warning(pop)
#pragma pack(pop)
#endif // _STL_COMPILER_PREPROCESSOR
#endif // _CSTRING_
What could be the problem here? Is there maybe a header conflicting from my current path includes with Visual Studio headers?
I tried
mex -setup c
mex -setup cpp but both didn't work. Any help would be much appreciated.
Thank you

Risposte (1)

Jack
Jack il 4 Mar 2025
This issue appears to be caused by a conflict between MATLAB’s generated code and the Visual Studio 2019 MSVC compiler's standard headers, specifically with the cstring file. Here’s how you can systematically debug and resolve the issue.Possible Causes & Solutions:1. Ensure That the Correct Compiler Version is Set in MATLAB
  • Verify that MATLAB is using the correct Microsoft Visual Studio (MSVC) compiler: mex -setup C
mex -setup C++
  • If multiple versions of Visual Studio are installed, MATLAB might be picking an incorrect one. To force MATLAB to use a specific version, specify it explicitly: mex -setup C "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64\cl.exe"
  • Check MATLAB’s recognized compilers using: mex -setup
2. Check for Path Conflicts in Included Headers
  • Since the error occurs in the cstring file, it’s possible that there is a conflicting version of a standard C++ library in MATLAB’s include paths.
  • Run the following command to inspect the active include paths: getenv('INCLUDE')
  • Look for conflicting paths from an older or incompatible library (e.g., MATLAB’s internal MinGW compiler might be conflicting with MSVC).
  • If a conflicting path is found, temporarily remove it using: setenv('INCLUDE', '')
  • Then, retry building the S-Function.
3. Force MATLAB to Use the Correct Standard Library
Since cstring is a C++ standard library header, check if MATLAB is using incorrect C++ standard settings:
  • Open the Simulink Coder Configuration Parameters:
  • Go to Model Configuration (Ctrl+E in Simulink) → Code GenerationCustom Code.
  • Ensure that the Compiler Flags include -std=c++17 or -std=c++14 (depending on MSVC compatibility).
  • Alternatively, modify mex options manually: mex -v -DMATLAB_MEX_FILE -std=c++17 myfile.c
  • Force the compiler to use standard C headers instead of C++ headers:
  • Before compiling, add: #include <string.h>
instead of:
#include <cstring>
  • Explicitly define _STL_COMPILER_PREPROCESSOR before including cstring:#define _STL_COMPILER_PREPROCESSOR 1
#include <cstring>
4. Ensure That Simulink Is Not Generating Incompatible Code
  • Since the error happens during S-function code generation, regenerate the Simulink Coder cache: slprj rebuild
  • Delete any old code and rebuild:
  • Navigate to your simModelFile\simModel_sfcn_rtw directory and delete any previously generated .c and .mexw64 files.
  • Recompile with: slbuild('simModel')
5. Try an Older MSVC Version
  • Some users report that MSVC 2017 (v141 toolset) works more reliably with MATLAB R2021a than MSVC 2019.
  • If possible, try using an older version:
  • Open Visual Studio Installer, go to Modify, and install MSVC v141 toolset.
  • Then set MATLAB to use it: mex -setup C "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\cl.exe"
Final Steps
After making the changes, try recompiling your S-Function using:
mex -v simModel_sf.c
If the problem persists, please provide:
  • The full compilation log (mex -v output).
  • The specific MATLAB-generated code (simModel_sf.c) where the error occurs.
This should help pinpoint the exact issue. Let me know how it goes! 🚀
  2 Commenti
Mete Soylu
Mete Soylu il 7 Mar 2025
Thanks for the quick answer. I applied your suggestions 1,2,4 but I couldn't succeed.
#include <cstring> calls a C++ header because _STD_BEGIN and _STD_END are C++ macros.
I changed Configuration parameters --> Code Generation Settings to
C++ code generation and selected MSVC 2019 C++ compiler
then I wrote to command window
mex -setup C++
with these settings, when I tried auto generation of Simulink on my Simulink subsystem, this time, It didn't give the <cstring> error, it filled more code into simModel.cpp file, but it gave Library Link error. It couldn't find Library named libecatinterface_vcx64.lib
Error is:
LINK : fatal error LNK1181: cannot open input file 'C:\Program Files\Polyspace\R2021a\toolbox\slrealtime\simulink\blocks\dist\lib\libecatinterface_vcx64.lib'
I couldn't find any info about this library on the internet. I think, I am going to change my Visual Studio to older version as you said. That will ,it seems, solve my problem.
Than you so much.
Jack
Jack il 7 Mar 2025
I'm glad I could help if you need any other assistance let me know (follow me to message me directly about any possible future problems) !

Accedi per commentare.

Categorie

Scopri di più su Simulink Coder in Help Center e File Exchange

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by