Splitting code generation and building MEX into two steps

Question primarily intended towards MATLAB staff, But answers from the helping community are welcome
I typically use codegen command with a mex config object to generate MEX file for distribution in my org. I also notice that the MATLAB versions used by different teams/individuals in my org be different (But all in Windows platform).
I can see that that there is an option in the codegen configuration object to only generate code (genCodeOnly) or also build the (mex) object by setting false or true.
Question 1) Why can't matlab allow only code generation using the latest version (say 2024a), and build a mex file using an older version (say 2018a) - In that case, I would reasonably expect (but not guaranteed) the MEX file generated in 2018a to work in all future releases, i.e why not allow me to split it into two different steps? If this is possible - How to do it?
Question 2) Lets say I generate only code using 2024a (and not a mex file), I can see that the mex option can already handle a release (-R2018a) option. How to use such option to only build mex on a previously generated code by codegen?

댓글 수: 9

Hi Sri Raman A,
1) The code generated for 'mex' configuration using the 'codegen' command will depend on the version of MATLAB. This is because the generated code utilizes MATLAB Library functions, which may vary depending on the MATLAB version. Refer to the following MATLAB documentation, https://www.mathworks.com/help/matlab/matlab_external/version-compatibility.html
There is an existing workflow of generating only code and later compiling the files to generate a mex file, usually with the help of a batch file and a makefile. However, this workflow is not useful in this case, since the generated code uses version-specific library calls and may not compile in other versions.
To generate the mex file from the generated code, simply running the batch file named "$(outputMexName)_mex.bat" is sufficient. To generate the mex file from the generated code after moving the code to another system, the "environment setup" batch file and makefile should be modified accordingly.
2) The 'mex' command is used to generate the mex file from the given 'C' or 'C++' source file. The flag (-2018a) is used to specify the release-specific API. This option won't be useful for the current problem.
Thanks for your reply.
I didnt know that the generated (c) code for mex configuration itself uses MATLAB version-specific library calls. Therefore, I am surprised why it is built like this.
For eg., When I generate code for building a Shared_Library (DLL) or a Standalone executable (EXE) - The generated DLLs and EXE's work on any machine irrespective of MATLAB version as long as the platform/operating system is same.
So, why mex configuration is built in such a way that it is not flexible enough to be used in different versions?
Infinite_king
Infinite_king 2024년 4월 15일
편집: Infinite_king 2024년 4월 15일
The use case of 'dll,' 'exe,' or 'lib' configurations is to generate code or executables that will run independently from MATLAB. However, the primary goal of 'mex' is to utilize MATLAB while decreasing execution time. Hence, the generated code for the 'mex' workflow still uses MATLAB Library calls, whereas other workflows will not. One advantage of using MATLAB compared to independent executions is that the 'mex' workflow supports GUI functions like plotting, whereas other workflows do not support them.
Sri Raman A
Sri Raman A 2024년 4월 15일
편집: Sri Raman A 2024년 4월 15일
So, Is there a way for me to generate code using 'dll' or 'exe' or 'lib' configs and build mex file using these generated code with the help of ninja/mingw64/gcc? So that my mex will actually be distribute-able to different MATLAB version users?
I dont think it is the right support article. The link you provided is for MATLAB Compiler. But what we are discussing here is MATLAB Coder. Can you please let me know if there a MATLAB Coder specific solution?
Infinite_king
Infinite_king 2024년 4월 16일
편집: Infinite_king 2024년 4월 16일
The simplest workaround that I can think of is to share the source code instead of the mex file and ask them to compile it to mex in their version of MATLAB or supply a simple script to compile the function into mex. Instead of a plain script file, a P-code file can be shared to make the source file unreadable.
Does it mean that everyone in the org is expected to have MATLAB Coder license on top of the regular MATLAB license?
For the workaround that i provided, MATLAB coder is needed to compile the P-code to mex.
Contact Technical support, you may get better workaround :- https://www.mathworks.com/support/contact_us.html

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 MATLAB Coder에 대해 자세히 알아보기

제품

릴리스

R2024a

태그

질문:

2024년 4월 10일

댓글:

2024년 4월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by