Generate/Link .mexw64 from multiple .obj-files

2 vues (au cours des 30 derniers jours)
Matthias Frontull
Matthias Frontull le 20 Fév 2024
Commenté : James Tursa le 7 Mar 2024
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

Réponse acceptée

Ayush
Ayush le 1 Mar 2024
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 commentaires
Matthias Frontull
Matthias Frontull le 4 Mar 2024
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 le 7 Mar 2024
Note that if your build also includes library files you can just list them in the mex command line as well.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by