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++"
How to set CFLAG /MT of Visual Studio compiler in S-Function builder
9 views (last 30 days)
Show older comments
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?
0 Comments
Answers (2)
Pratyush
on 14 Dec 2023
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
- Change the "Runtime Library" setting to "Multi-threaded DLL (/MD)" for Release configuration.
- Rebuild the static library.
- Now try building your S-function again in MATLAB/Simulink.
I hope this helps.
See Also
Categories
Find more on Block and Blockset Authoring in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!