Generate/Link .mexw64 from multiple .obj-files

조회 수: 4 (최근 30일)
Matthias Frontull
Matthias Frontull 2024년 2월 20일
댓글: James Tursa 2024년 3월 7일
Hello,
im am trying to generate a mex-file from my c-Code.
My code includes .c Files and .cpp Files, but have to be generated with the gcc compiler, not with the g++ in order to work.
However, if the makefile includes .cpp files, Matlab switches automatically to the g++ compiler.
My idea was now to generate the .obj file individually/manually with the correct compiler and then link them also manually to mex-file.
But I am not sure if this "manual linking" is even possible in Matlab?
Best regards
Matthias

채택된 답변

Ayush
Ayush 2024년 3월 1일
Hi Matthais,
Just for your clarification, MATLAB actually uses both the compilers i.e “gcc” and “g++” under the hood for compiling the “.c” files and “.cpp” files respectively. After the object code is created, “gcc” is again used to build the MEX file. You can peer under the hood to see the execution of the “mex” function by using the “-v” flag. Here is a useful documentation that explains the whole process of creating, compiling, and linking MEX files in great detail:
Also, Yes, it is possible to generate the “.obj” file individually with the corresponding compilers and then link them to a MEX-file of your choosing in MATLAB. Please refer to the below MEX command for linking the two object files manually:
mex -output your_mexfile your_c_code.o your_cpp_code.o
Here is the documentation for the “mex” function for some additional information on how to build such files:
Hope it helps!
  댓글 수: 2
Matthias Frontull
Matthias Frontull 2024년 3월 4일
Hello Ayush,
thanks a lot. Yes it helps as the Mathworks documentation on the mex function was a little bit scarse regarding the linking. Or at least I could not extract the information I wanted.
But now I was able to link the files and generate the mexe!
James Tursa
James Tursa 2024년 3월 7일
Note that if your build also includes library files you can just list them in the mex command line as well.

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

추가 답변 (0개)

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by