Using mex -setup with MinGW64 C++ Compiler from a Custom Path

27 次查看(过去 30 天)
Hello,
I am trying to create an API between MATLAB and C++ so that my team members, who don't have a MATLAB license, can use it with just the MATLAB Runtime. I used the C++ shared library to generate the API. After downloading MINGW8.1 and the Windows SDK, and setting the appropriate paths in my environment, I successfully compiled my example.cpp file.
However, when I tried to install my application on another computer that doesn't have MATLAB, I encountered an issue. Despite installing MINGW8.1, the Windows SDK, and my API application, the compiler was not found. Using the mex -setup command in the command prompt in the computer with MATLAB, I noticed that my computer uses compilers located in the .../MATLAB/... directory. It seems that I still need MATLAB to compile a standalone API.
Can I change the compiler path, and if so, how can I do it?
C:\Users\UserName>mex -setup c++
MEX configured to use 'MinGW64 Compiler with Windows 10 SDK or later (C++)' for C++ language compilation.
To choose a different C++ compiler, execute one from the following:
For MinGW64 Compiler (C++): mex -setup:"C:\Program Files\MATLAB\R2024a\bin\win64\mexopts\mingw64_g++.xml" C++
For MinGW64 Compiler with Windows 10 SDK or later (C++): mex -setup:C:\Users\User\AppData\Roaming\MathWorks\MATLAB\R2024a\mex_C++_win64.xml C++
For Microsoft Visual C++ 2022: mex -setup:"C:\Program Files\MATLAB\R2024a\bin\win64\mexopts\msvcpp2022.xml" C++
Additionally, I am trying to connect my API with another C++ program, such as OpenFOAM, which requires a different C++ compiler. Do I need to compile both .cpp files separately with different compilers, or is there a better solution to this problem?

回答(1 个)

Pratyush
Pratyush 2024-7-9
Hi Xueqi,
To create an API between MATLAB and C++ that can be used without MATLAB, follow these steps:
Changing the Compiler Path
Locate and Edit the 'mexopts' XML File:
  • Find the XML file path using 'mex -setup'.
  • Edit the <COMPILER> tag to point to your installed compiler.
Set the Environment Variable:
  • Set 'MW_MINGW64_LOC' to your MinGW installation path.
Compiling the API for Use Without MATLAB
  • Ensure the target computer has MATLAB Runtime installed.
  • Link against MATLAB Runtime libraries instead of the full MATLAB installation.
  • Include all required DLLs from MATLAB Runtime in your distribution package.
Integrating with Another C++ Program (e.g., OpenFOAM)
  • Compile the MATLAB API with MinGW and the other program with its required compiler.
  • Configure both to use a common supported compiler.
  • Create a wrapper layer in C++ compiled with MinGW for use by the other program.
Hope this helps.

Community Treasure Hunt

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

Start Hunting!

Translated by