Can I link a library or object file built with one compiler to a MEX function built with a different compiler?
8 views (last 30 days)
Show older comments
MathWorks Support Team
on 29 May 2024
Edited: MathWorks Support Team
on 14 Sep 2024 at 22:09
I am using MATLAB R2024a on a Windows machine to build and link a C++ MEX function to a C++ static library.
I am compiling the static library outside of MATLAB using a supported version of the MinGW C/C++ compiler. In MATLAB, I am using the "mex" function and a supported version of the Microsoft Visual C++ compiler.
Is this a supported workflow? And if not, what workaround is available for me?
Accepted Answer
MathWorks Support Team
on 14 Sep 2024 at 0:00
The compiler used to build object files or libraries that are linked to your "mex" function must be compatible with the compiler you are using to compile your "mex" function.
Microsoft Visual C/C++ is not compatible with the MinGW compiler, but is compatible with Intel compilers. For example, you can use an Intel compiler to build a Fortran/C/C++ library and link it to a "mex" function that is compiled with Microsoft Visual C/C++. Note that, by default, "mex" is not configured to search for Intel's library files when using the Microsoft Visual C/C++ compiler, so you may need to include Intel library paths in the "mex" command if you use the Intel compiler to build linked files. See this article for more information: https://www.mathworks.com/matlabcentral/answers/2123811-why-do-i-get-lnk1104-cannot-open-file-ifmodintr-lib-using-mex-with-a-fortran-file
If you are using MinGW to compile your "mex" function, then you must use MinGW to compile object files and libraries that you are linking to your "mex" function. For example, you can use MinGW-w64 to build a C/C++ library and link it to a "mex" function that is compiled with MinGW-w64. Note that, starting in R2024a, you can also use MinGW-w64 to build Fortran library or object files that can be linked to a "mex" function that is compiled with MinGW-w64.
For more information, please refer to the following documentation pages detailing the supported compilers on Windows, how to install the MinGW-w64 compiler, and how to set up the "mex" function to use this compiler:
https://www.mathworks.com/help/matlab/matlab_external/changing-default-compiler.html
Here is a high-level overview of a workflow using MinGW:
1. Install a supported version of MinGW-w64 by following the steps highlighted in the above documentation page.
2. Build the static library using MinGW-w64.
3. Set up the "mex" function to use the installed compiler by following the steps highlighted in the above documentation page.
4. Build the MEX function using "mex" and pass the C++ file and static library file as "filenames" arguments. For more information on the "filenames" input argument to "mex", please refer to the following section of the "mex" documentation page:
0 Comments
More Answers (0)
See Also
Categories
Find more on MATLAB Support for MinGW-w64 C/C++ Compiler 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!