To compile HelloWorld.c, we simply run mex HelloWorld.c from the MATLAB interpreter. We can obtain additional information about the compilation, such as the compiler and compiler flags being used, by passing the -v option to mex. A great deal of extra information is then printed in the MATLAB interpreter, perhaps most importantly, the location of the options file where all of the mex options are defined.


>> mex -v HelloWorld.c
-> mexopts.sh sourced from directory
   FILE = /opt/apps/matlab/r2009a/bin/mexopts.sh
--------------------------------------------------
-> MATLAB       = /opt/apps/matlab/r2009a
-> CC           = gcc
-> CC flags:
   CFLAGS       = -ansi -D_GNU_SOURCE
                  -fexceptions -fPIC
                  -fno-omit-frame-pointer
                  -pthread
...

The compiled file will have the same base filename as the C file, and a system-dependent extension. For example, on a relatively standard Linux installation, the compiled file was named HelloWorld.mexa64. In addition, you can call the compiled function from the MATLAB interpreter or from other MATLAB functions with the base name.


>> HelloWorld
Hello World

Note: if both HelloWorld.m and HelloWorld.mexa64 are in the same directory, and you call HelloWorld from the MATLAB interpreter, the compiled function will take precedence. We stress again that it is up to the mex writer to adequately handle unrecognized calling sequences coming from the MATLAB interpreter. In a later example in this topic, we will go into more detail on the error checking code which may be used for this purpose.

 
©   Cornell University  |  Center for Advanced Computing  |  Copyright Statement  |  Inclusivity Statement