MATLAB: How to create a shared library which does not use a display with MATLAB Compiler

MATLAB Compiler

I would like to create a shared library with MATLAB Compiler which does not need a display, since my application is going to run in an environment where no display is available. When I run the application, however, the following warning message is always displayed:
Warning: Unable to open display , MATLAB is starting without a display.
You will not be able to display graphics on the screen.

Best Answer

The ability to compile a shared library which disables the display is not available in MATLAB Compiler.
The workaround for accomplishing this differs depending on which release you are using.
For R2010b and later:
The -nodisplay option cannot be specified within the shared library itself; rather, it must be specified by the caller of the library. For these releases, -nodisplay can be passed as an argument to mclInitializeApplication similarly to other command line arguments such as -nojvm or -nojit.
An example of this, as well as a list of valid command line switches can be found here.
For R2010a and earlier:
For these releases the -nodisplay option cannot be specified in the same way as other command line arguments such as -nojvm or -nojit which are passed as arguments to mclInitializeApplication. Instead, you can modify the generated wrapper code to specify the "-nodisplay" option.
Follow these steps to create a shared library which will run in NO DISPLAY mode:
1) Create the wrapper file for the library. This is done by leaving off the link options in the MCC command:
mcc -W lib:libfoo foo.m
2) Open the file "<libname>_mcc_component_data.c" where "<libname>" is the name of the library specified in step 1 (eg libfoo_mcc_component_data).
3) Set MCC_<libname>_run_opts_data[] to { "-nodisplay" }. For example, you would change the lines
static const char * MCC_libfoo_run_opts_data[] =
{ "" };
to
static const char * MCC_libfoo_run_opts_data[] =
{ "-nodisplay" };
4) Set the number of MCC_<libname>_run_opts_data to 1. For example, change the lines:
/* MCR global runtime options */
MCC_libfoo_run_opts_data,
/* Number of MCR global runtime options */
0,
to
/* MCR global runtime options */
MCC_libfoo_run_opts_data,
/* Number of MCR global runtime options */
1,
5) Complete the compilation by using MBUILD. (Note that you can find out this compile command by compiling the library with mcc using the -v option: "mcc -v -W lib:libfoo -T link:lib foo.m")
mbuild -O -output "libfoo" "libfoo.c" "libfoo.exports" "libfoo_mcc_component_data.c" -link shared
6) Create the driver application as normal. You do not need to pass any arguments to mclInitializeApplication (eg use mclInitializeApplication(NULL, 0);).
7) Compiled the application with MBUILD as usual. For example:
mbuild callfoo.c -L. -lfoo -I.
The application should now run without the warning.
Please note that if you are creating a standalone executable you can use the option "-R -nodisplay" in the mcc command. For example:
mcc -R -nodisplay -m foo.m