How to set CFLAG /MT of Visual Studio compiler in S-Function builder

I'm trying to include libraries (.lib files), which I build with Visual Studio 2022 in the S-function builder.
Therefor I added the Path to the include foulder as LIB_PATH and the name of the libraries as ENTRY.
For example:
ENV_PATH $PROJROOT
LIB_PATH $PROJROOT\protobuf_src\lib
ENTRY libprotobuf.lib
(the environment variable $PROJROOT was set to my projectfoulder before)
When I try to build in the S-function builder I get the error:
libprotobuf.lib(arena.obj) : error LNK2038: Konflikt ermittelt für "RuntimeLibrary": Der Wert "MT_StaticRelease" stimmt nicht mit dem Wert "MD_DynamicRelease" in tpa18e8d92_2cfe_4009_b8ad_da46fef4bb35.obj überein.
In mex -setup c++ I chose the "Microsoft Visual C++ 2022" compiler.
I also once had a similar problem when building a other project in Visual Studio 2022, there I could solve it by setting the CFLAG from "Multithreaded-DLL(/MD)" to "Multithreaded(/MT)". Is there some similar solution for Matlab/Simulink?

Answers (2)

I just found the answer. To change the CFLAGs the mex_C++_win64.xml - file has to be edited futher information about it's location with "mex -setup c++"
Hi Lucas,
I understand that you are getting error when you build a S-function that includes a library build in MSVC2022.
It seems you're encountering a runtime library conflict error (LNK2038) when trying to build an S-function in MATLAB/Simulink that links against a static library (libprotobuf.lib). The error indicates that "libprotobuf.lib" was compiled with the "/MT" flag (static runtime library), whereas the S-function appears to be compiled with the "/MD" flag (dynamic runtime library).
To solve this issue, you need to ensure that both your static libraries and the S-function are compiled with the same runtime library setting. Since MATLAB typically uses the "/MD" flag for dynamic linking.
You need to rebuild your libraries with "/MD" flag. In Visual Studio
  1. Change the "Runtime Library" setting to "Multi-threaded DLL (/MD)" for Release configuration.
  2. Rebuild the static library.
  3. Now try building your S-function again in MATLAB/Simulink.
I hope this helps.

1 Comment

Hi!
Thank you, but unfortunately this doesn't really help because i have to include .lib files and will run into linking problems when using (/MD).
But like I wrote in the comment I already solved it by changing the CFLAGs in the mex_C++_win64.xml - file.
Best regards.

Sign in to comment.

Categories

Find more on Block and Blockset Authoring in Help Center and File Exchange

Products

Release

R2023b

Asked:

on 14 Dec 2023

Edited:

on 28 Dec 2023

Community Treasure Hunt

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

Start Hunting!