Search code examples
c++cmatlabmex

mexPrintf message doesn't show on matlab command window


I started working with mex on matlab to build a c code and I wrote a very simple code (main.c) to begin with :

#include "stdio.h"
#include "stdlib.h"
#include "mex.h"

void main()
{
    mexPrintf("Hello world");
}

when I type mex main.c in a matlab script everything goes well and I have this message : "Building with 'gcc'. MEX completed successfully." but I don't see the message "Hello world", I tried printf() too without success, does anyone know why the message doesn't appear on the matlab window please ?

Thanks in advance for your help.

-J


Solution

  • Your code, as it stands, is not compatible with Matlab. Matlab requires a special function definition for the 'main' function of compiled programs.

    In order to run your code, you will need to have something like this:

    #include "stdio.h"
    #include "stdlib.h"
    #include "mex.h"
    
    void mexFunction( int nlhs, mxArray *plhs[],
              int nrhs, const mxArray*prhs[] )
    {
        mexPrintf("Hello world\n");
    }
    

    Assuming that this is placed in a file entitled, 'helloWorld.c', you can run the following commands at the Matlab prompt:

    mex helloWorld.c
    helloWorld