Resolving WinAC Target MATLAB/Simulink ert_main.cpp Error C3861

David Krause11 min read
Other TopicSiemensTroubleshooting
Licensed PE Working through this on a live machine? A Maine-licensed engineer can take it from here — included with IMD hardware, by the hour for everything else. Book an engineer

Problem Overview

Engineers integrating MATLAB/Simulink models into a Siemens PC-based controller via the WinAC Target add-on occasionally encounter a Visual C++ compiler failure during the model build phase. The build process starts correctly: the Simulink model is parsed, the WinAC Target wrapper code is generated, and the Microsoft Visual Studio toolchain is invoked. The compile then aborts with a single unresolved-symbol error reported against the file ert_main.cpp at line 112:

ert_main.cpp(112) : error C3861: 'rtERTExtModeParseArgs': identifier not found

This error is reported by the Visual C++ compiler (cl.exe) when the generated embedded real-time (ERT) main file contains a call to rtERTExtModeParseArgs but the linker cannot resolve the symbol because the function is no longer exported by the Simulink Coder / Embedded Coder runtime library that ships with the installed MATLAB release. The result is a failed build, no .dll is produced, and the model cannot be loaded by the WinAC RTX controller through STEP 7 and the WinAC ODK interface.

The error is reproducible on a clean installation of MATLAB R2013a combined with WinAC Target V1.2 SP1, WinAC ODK V4.2 SP1, and Microsoft Visual Studio 2010 Professional on Windows 7 32-bit. It has also been reproduced on later MATLAB releases with the same WinAC Target version.

Affected Software Versions and Toolchain

The failure has been confirmed on the following combination, which is the configuration that ships with the official Siemens application example "Calling Simulink models using STEP 7 and WinAC ODK" (entry ID 56969417):

Component Version in Failing Configuration Version Verified Working
MATLAB R2013a R2012a
MATLAB Coder V2.3 V2.2 or earlier
Simulink Coder V8.3 V8.2 or earlier
Embedded Coder V6.3 V6.2 or earlier
WinAC Target V1.2 (SP1) V1.2 (SP1)
WinAC ODK V4.2 SP1 V4.2 SP1
Visual Studio 2010 Professional 2010 Professional
Windows 7 32-bit 7 32-bit

The function rtERTExtModeParseArgs was a documented entry point of the Simulink Coder external-mode runtime that parsed command-line arguments passed to the generated executable when the model was configured to communicate with the Simulink host via the external-mode TCP channel. Beginning with MATLAB R2013a, MathWorks renamed and re-implemented the external-mode argument parsing routine. The WinAC Target V1.2 templates still emit a call to the legacy name, while the R2013a runtime libraries no longer export the legacy symbol. Hence the C3861 unresolved identifier.

Root Cause Analysis

There are two distinct but interacting root causes:

  1. External mode enabled in the Simulink model configuration. When the model's Code Generation settings specify an external mode (Simulation → Model Configuration Parameters → Code Generation → Interface → External mode), the Embedded Coder template generates an ert_main.cpp that calls rtERTExtModeParseArgs(argc, argv). This call exists so that the generated binary can interpret host-side parameters such as TCP port, verbose flag, and target identifier when running under grt_main or ert_main.
  2. Symbol removal in newer Simulink Coder runtime libraries. MathWorks renamed the argument-parsing helper in newer MATLAB releases. The function body and signature were either inlined into a different module or removed in favor of a configuration-object-driven parser. The header file ext_mode.h still references the legacy name in template sources that ship with WinAC Target V1.2, while the import library libmwcoder.lib (or equivalent) no longer provides the symbol.

Both causes must be addressed to obtain a successful build. The first can be addressed by the user by toggling a checkbox in the Simulink model. The second requires either a downgrade of the MATLAB release or a patch to the WinAC Target template.

Verification of the Root Cause

To prove that the external-mode linkage is the trigger, disable external mode in the model and rebuild:

  1. Open the Simulink model.
  2. Select Simulation → Model Configuration Parameters.
  3. Navigate to Code Generation.
  4. Click Interface.
  5. Uncheck External mode.
  6. Click Apply, then OK.
  7. Trigger the WinAC Target build (e.g., from the MATLAB command window: rtwbuild('myModel') with the WinAC Target system target file selected).

If the build completes and produces myModel.dll in the working folder, the root cause is confirmed as the external-mode/legacy-symbol mismatch. The user-reported reproduction confirms that this exact procedure resolves the C3861 failure on MATLAB R2013a with WinAC Target V1.2 SP1.

Solution 1 - Disable External Mode (Recommended)

The cleanest fix is to build the WinAC Target output without external mode. The WinAC Target architecture does not require external mode for production deployment on the RTX controller. External mode is only useful when an engineer wants to tune parameters live from the Simulink host, which is rarely needed when the model has been transferred to STEP 7 and is executed on WinAC RTX.

Step-by-Step Procedure

  1. Open the Simulink model that targets the WinAC RTX via WinAC Target.
  2. Open Model Configuration Parameters from the Modeling tab.
  3. In the left pane, click Code Generation.
  4. Confirm the active system target file is winac.tlc (WinAC Target). If not, click Browse and select the WinAC Target TLC shipped with the WinAC Target add-on.
  5. Click the Interface sub-node.
  6. Uncheck the External mode checkbox.
  7. Click Apply.
  8. From the MATLAB command window execute:
    rtwbuild(gcs)
    or invoke the build through the WinAC Target menu/toolbar action.
  9. Monitor the build log. The ert_main.cpp error should no longer appear.
  10. Locate the produced .dll in the model's build directory and copy it to the STEP 7 project for use with the SFB / ODK call.

Solution 2 - Use a Compatible MATLAB Release

If external mode is mandatory (for example, for live tuning of a control loop on a development bench), the alternative is to use a MATLAB release that still ships the legacy rtERTExtModeParseArgs symbol. Releases confirmed to build cleanly with WinAC Target V1.2 include:

  • MATLAB R2012a with Simulink Coder V8.2 and Embedded Coder V6.2.
  • MATLAB R2011a/b with the corresponding earlier versions.

On these releases, the same Simulink model with external mode enabled builds successfully with no source modification. The WinAC Target V1.2 documentation and example projects are validated against these versions.

Solution 3 - Patch the WinAC Target Template (Advanced)

For users who must remain on MATLAB R2013a or later and who also require external mode, the following advanced workaround can be applied. The WinAC Target templates are stored in the MATLAB installation under the folder selected during WinAC Target installation (typically <MATLAB>\toolbox\winac\winactarget\winac\).

  1. Locate the file ert_main.cpp.tlc inside the WinAC Target template folder.
  2. Find the call site for rtERTExtModeParseArgs. It typically appears inside a guard such as:
    #if EXT_MODE
        rtERTExtModeParseArgs(argc, argv);
    #endif
  3. Replace the call with the equivalent call exported by the new Simulink Coder runtime. The replacement depends on the MATLAB release; for R2013a and R2013b the equivalent is:
    #if EXT_MODE
        rtExtModeParseArgs(argc, argv);
    #endif
  4. Save the TLC file and rebuild. Note that this modification is performed at the user's own risk; Siemens has not published an official patch for this symbol mismatch in WinAC Target V1.2.

After patching, regenerate code by issuing rtwbuild(gcs) and confirm that the build completes without C3861.

Build Environment Sanity Checks

Before rebuilding, verify the toolchain is consistent. Mixed installations of MATLAB, Visual Studio, and Windows SDKs are a common cause of cryptic linker errors.

Check Command / Location Expected Result
MATLAB recognized Visual Studio 2010 !mex -setup in MATLAB Shows Visual Studio 2010 Professional as default C compiler
WinAC Target installed ver in MATLAB Lists "WinAC Target" in the toolbox list
WinAC ODK installed Programs and Features "SIMATIC WinAC ODK V4.2 SP1" present
Target file winac.tlc registered which -all winac.tlc Returns the full path under the WinAC Target install
Visual C++ cl.exe on PATH for MATLAB session !where cl Resolves to Visual Studio 2010 VC\bin\cl.exe
Windows SDK referenced MATLAB env: setenv('WindowsSdkDir', ...) Points to Windows SDK 7.1 (matches VS2010)

If mex -setup reports the LCC compiler bundled with MATLAB or any other compiler, switch it back to Microsoft Visual C++ 2010 Professional before invoking the WinAC Target build. The LCC compiler cannot link against the WinAC ODK import libraries and will produce additional errors.

Integrating the Built DLL into STEP 7

Once the build succeeds, the generated .dll must be made known to the STEP 7 project so that the WinAC RTX controller can call it via WinAC ODK. The standard flow (per the Siemens application example, see the documentation PDF 56969417_WinAC_Target_DOCU_V151_en.pdf) is:

  1. Copy the generated .dll into the STEP 7 project's omponents\WinAC ODK folder or the equivalent custom path referenced by the ODK SFB.
  2. In the STEP 7 program, instantiate the ODK SFB that calls the DLL (for example, SFB 34950 "ODK_CALL" or a customer-specific wrapper).
  3. Supply the function name as exported by the DLL using the WinAC Target naming convention <model_name>_step or <model_name>_init, depending on whether you call the periodic step function or the one-time initialization.
  4. Download the STEP 7 project to the WinAC RTX target (e.g., a CPU 416 with WinRTx slot PLC, or a WinAC RTX software controller).
  5. Place the controller in RUN. The ODK runtime loads the DLL and invokes the exported functions at the configured cycle.

Verification Procedure

To verify the integrated Simulink model is executing correctly on WinAC RTX, perform the following checks in order:

  1. Build artifact exists. Confirm <model>.dll is present in the build folder and has a recent timestamp.
  2. Exports present. Open the DLL with Dependency Walker (depends.exe) and verify it exports <model>_step and, if configured, <model>_init.
  3. STEP 7 download completes. The WinAC RTX online view should report a clean download with no ODK errors in the diagnostic buffer (entries with event ID 0x135E or 0x135F indicate DLL load problems).
  4. Watchdog OK. The WinAC RTX status panel should report the ODK call cycle active and no watchdog timeouts (event ID 0x4380 series).
  5. Signal trace. From the Simulink host, if external mode is enabled (post-fix), parameter values should update live. Without external mode, verify outputs by reading the corresponding process image tags in STEP 7 or via a connected HMI.

Troubleshooting Matrix

Symptom Likely Cause Corrective Action
C3861 'rtERTExtModeParseArgs' External mode enabled with newer MATLAB release Disable external mode OR downgrade MATLAB OR patch TLC
cl.exe not recognized Wrong compiler selected in mex -setup Switch default compiler to Visual Studio 2010
LINK : error LNK2019 unresolved external on WinAC ODK symbols WinAC ODK include/lib paths not in project Verify WinAC ODK V4.2 SP1 installation and that winac.tlc template references correct SDK paths
fatal error C1083: Cannot open include file: 'windows.h' Windows SDK not visible to MATLAB build Set WindowsSdkDir environment variable or run MATLAB from the VS2010 command prompt
Build succeeds, DLL not loaded on RTX (event 0x135E) Function name mismatch or 64/32 bit mismatch Confirm both STEP 7 and the build host are the same bitness (32-bit for RTX on Win7)
rtwbuild fails with TLC error WinAC Target template not on MATLAB path Re-run WinAC Target installer and re-register the target file

Best Practices for WinAC Target / MATLAB Integration

  • Pin your MATLAB release. WinAC Target V1.2 is validated against MATLAB R2012a. If you upgrade MATLAB for other reasons, plan to either disable external mode or wait for a WinAC Target revision that matches the new Simulink Coder runtime.
  • Keep the build environment reproducible. Use a virtual machine or a documented baseline image so that changes to MATLAB, Visual Studio service packs, or Windows updates do not silently break the code generation.
  • Prefer code generation without external mode for production builds. External mode adds runtime overhead and a TCP listener that is unnecessary once the model is deployed.
  • Version the generated DLL. Include a build timestamp or Git hash in the model and have WinAC Target inject it as a preprocessor symbol so that the STEP 7 project always knows which Simulink revision is running.
  • Test on the actual WinAC RTX target. Behavior in MATLAB simulation differs from behavior on the RTX real-time scheduler. Always run a representative scenario on the real hardware (or on WinLC RTX in a lab VM) before deploying.

FAQ

What causes the C3861 'rtERTExtModeParseArgs' error in WinAC Target builds?

The error is raised when the Simulink model is configured with external mode enabled and the installed MATLAB release (typically R2013a or newer) no longer exports the legacy rtERTExtModeParseArgs symbol from its Simulink Coder runtime library. The WinAC Target V1.2 template still emits a call to this symbol, so the Visual C++ linker cannot resolve it.

Which MATLAB version is officially supported by WinAC Target V1.2?

The WinAC Target V1.2 documentation validates MATLAB R2012a with Simulink Coder V8.2 and Embedded Coder V6.2. Builds on R2013a and later can succeed only if external mode is disabled or the TLC template is patched.

Can I keep external mode enabled on a newer MATLAB release?

Yes, by patching the ert_main.cpp.tlc template inside the WinAC Target install folder to call the renamed runtime function (e.g., rtExtModeParseArgs) instead of the legacy symbol. This modification is not officially supported by Siemens.

Do I need Visual Studio 2010 for WinAC Target builds?

Yes, the WinAC Target V1.2 template is configured for the Visual Studio 2010 Professional MSVC toolchain. Newer Visual Studio versions will cause the Visual C++ cl.exe to refuse or miscompile the generated ERT main. Configure mex -setup in MATLAB to point at the VS2010 compiler before running rtwbuild.

Where do I find the official Siemens documentation for this integration?

The application example "Calling Simulink models using STEP 7 and WinAC ODK" (entry ID 56969417) on the Siemens Industry Online Support portal provides the workflow. The documentation PDF 56969417_WinAC_Target_DOCU_V151_en.pdf is the primary reference, and the entry page 56969417 links to the WinAC Target add-on description.

Back to blog