Custom Building Binary MEX-Files

Who Should Read This Chapter

In general, the defaults that come with MATLAB® software should be sufficient for building most binary MEX-files. Following are reasons that you might need more detailed information:

The script, in general, uses two stages (or three, for Microsoft® Windows® platforms) to build MEX-files. These are the compile stage and the link stage. In between these two stages, Windows compilers must perform some additional steps to prepare for linking (the prelink stage).

MEX Script Switches

The mex script has a set of switches (also called options) that you can use to modify the link and compile stages. The MEX Script Switches table lists the available switches and their uses. Each switch is available on both UNIX®[1] and Windows systems unless otherwise noted.

For customizing the build process, you should modify the options file, which contains the compiler-specific flags corresponding to the general compile, prelink, and link steps required on your system. The options file consists of a series of variable assignments; each variable represents a different logical piece of the build process.

MEX Script Switches

SwitchFunction

@<rsp_file>

(Windows systems only) Include the contents of the text file <rsp_file> as command-line arguments to mex.

-<arch>

Build an output file for architecture <arch>. To determine the value for <arch>, type computer('arch') at the MATLAB Command Prompt on the target machine. Valid values for <arch> depend on the architecture of the build platform.

-ada <sfcn.ads>

Use this option to compile a Simulink® S-function written in Ada, where <sfcn.ads> is the Package Specification for the S-function. When this option is specified, only the -v (verbose) and -g (debug) options are relevant. All other options are ignored. For examples and information on supported compilers and other requirements, see README in the simulink/ada/examples directory.

-argcheck

(C functions only) Add argument checking. This adds code so arguments passed incorrectly to MATLAB API functions cause assertion failures.

-c

Compile only. Creates an object file, but not a binary MEX-file.

-compatibleArrayDims

Build a binary MEX-file using the MATLAB Version 7.2 array-handling API, which limits arrays to 2^31-1 elements. This option is the default. (See also the -largeArrayDims option.)

-cxx

(UNIX systems only) Use the C++ linker to link the MEX-file if the first source file is in C and there are one or more C++ source or object files. This option overrides the assumption that the first source file in the list determines which linker to use.

-D<name>

Define a symbol name to the C preprocessor. Equivalent to a #define <name> directive in the source.

-D<name>=<value>

Define a symbol name and value to the C preprocessor. Equivalent to a #define <name> <value> directive in the source.

-f <optionsfile>

Specify location and name of options file to use. Overrides the mex default-options-file search mechanism.

-fortran

(UNIX systems only) Specify that the gateway routine is in Fortran. This option overrides the assumption that the first source file in the list determines which linker to use.

-g

Create a binary MEX-file containing additional symbolic information for use in debugging. This option disables the mex default behavior of optimizing built object code (see the -O option).

-h[elp]

Print help for mex.

-I<pathname>

Add <pathname> to the list of directories to search for #include files.

-inline

Inline matrix accessor functions (mx*). The generated MEX-function may not be compatible with future versions of MATLAB.

-l<name>

Link with object library. On Windows systems, <name> expands to <name>.lib or lib<name>.lib and on UNIX systems, to lib<name>.so or lib<name>.dylib.

-L<directory>

Add <directory> to the list of directories to search for libraries specified with the -l option. On UNIX systems, you must also set the run-time library path, as explained in Setting Run-Time Library Path.

-largeArrayDims

Build a binary MEX-file using the MATLAB large-array-handling API. This API can handle arrays with more than 2^31–1 elements when compiled on 64-bit platforms. (See also the -compatibleArrayDims option.)

-n

No execute mode. Print any commands that mex would otherwise have executed, but do not actually execute any of them.

-O

Optimize the object code. Optimization is enabled by default and by including this option on the command line. If the -g option appears without the -O option, optimization is disabled.

-outdir <dirname>

Place all output files in directory <dirname>.

-output <resultname>

Create binary MEX-file named <resultname>. The appropriate MEX-file extension is automatically appended. Overrides the default MEX-file naming mechanism.

-setup

Interactively specify the compiler options file to use as the default for future invocations of mex by placing it in the user profile directory (returned by the prefdir command). When this option is specified, no other command-line input is accepted.

-U<name>

Remove any initial definition of the C preprocessor symbol <name>. (Inverse of the -D option.)

-v

Verbose mode. Print the values for important internal variables after the options file is processed and all command-line arguments are considered. Prints each compile step and final link step fully evaluated.

<name>=<value>

Supplement or override an options file variable for variable <name>. This option is processed after the options file is processed and all command line arguments are considered.

UNIX® Default Options File

The default MEX options file provided with MATLAB is located in matlabroot/bin. The mex script searches for an options file called mexopts.sh in the following order:

mex uses the first occurrence of the options file it finds. If no options file is found, mex displays an error message. You can directly specify the name of the options file using the -f switch.

The UNIX options file is written in the Bourne shell script language.

For specific information on the default settings for the MATLAB supported compilers, you can examine the options file in fullfile(matlabroot, 'bin', 'mexopts.sh'), or you can invoke the mex script in verbose mode (-v). Verbose mode prints the exact compiler options, prelink commands (if appropriate), and linker options used in the build process for each compiler. Custom Building on UNIX® Systems gives an overview of the high-level build process.

Windows® Default Options File

The default MEX options file is placed in your user profile directory after you configure your system by running mex -setup. The mex script searches for an options file called mexopts.bat in the following order:

mex uses the first occurrence of the options file it finds. If no options file is found, mex searches your machine for a supported C compiler and automatically configures itself to use that compiler. Also, during the configuration process, it copies the compiler's default options file to the user profile directory. If multiple compilers are found, you are prompted to select one.

On Windows systems, the options file is written in the Perl script language.

For specific information on the default settings for the MATLAB supported compilers, you can examine the options file, mexopts.bat, or you can invoke the mex script in verbose mode (-v). Verbose mode prints the exact compiler options, prelink commands, if appropriate, and linker options used in the build process for each compiler. Custom Building on Windows® Systems gives an overview of the high-level build process.

The User Profile Directory

The Windows user profile directory is a directory that contains user-specific information such as desktop appearance, recently used files, and Start menu items. The mex and mbuild utilities store their respective options files, mexopts.bat and compopts.bat, which are created during the -setup process, in a subdirectory of your user profile directory, named Application Data\MathWorks\MATLAB.

Custom Building on UNIX® Systems

On UNIX systems, there are two stages in MEX-file building: compiling and linking.

Compile Stage

The compile stage must

Link Stage

The link stage must

For Fortran MEX-files, the symbols are all lowercase and may have appended underscores. For specific information, invoke the mex script in verbose mode and examine the output.

Build Options

For customizing the build process, you should modify the options file. The options file contains the compiler-specific flags corresponding to the general steps outlined above. The options file consists of a series of variable assignments. Each variable represents a different logical piece of the build process. The options files provided with MATLAB are located in matlabroot/bin. The section UNIX® Default Options File, describes how the mex script looks for an options file.

To aid in providing flexibility, there are two sets of options in the options file that you can turn on and off with switches to the mex script. These sets of options correspond to building in debug mode and building in optimization mode. They are represented by the variables DEBUGFLAGS and OPTIMFLAGS, respectively, one pair for each driver that is invoked (CDEBUGFLAGS for the C compiler, FDEBUGFLAGS for the Fortran compiler, and LDDEBUGFLAGS for the linker; similarly for the OPTIMFLAGS):

Aside from these special variables, the mex options file defines the executable invoked for each of the three modes (C compile, Fortran compile, link) and the flags for each stage. You also can provide explicit lists of libraries that must be linked in to all MEX-files containing source files of each language.

The variable summary follows.

VariableC CompilerFortran CompilerLinker

Executable

CC

FC

LD

Flags

CFLAGS

FFLAGS

LDFLAGS

Optimization

COPTIMFLAGS

FOPTIMFLAGS

LDOPTIMFLAGS

Debugging

CDEBUGFLAGS

FDEBUGFLAGS

LDDEBUGFLAGS

Additional libraries

CLIBS

FLIBS

(none)

For specifics on the default settings for these variables, you can

Custom Building on Windows® Systems

There are three stages to MEX-file building for both C and Fortran on Windows systems: compiling, prelinking, and linking.

Compile Stage

For the compile stage, a mex options file must

Prelink Stage

The prelink stage dynamically creates import libraries to import the required function into the MEX, MAT, or engine file:

Link Stage

For the link stage, a mex options file must

Linking DLL Files to Binary MEX-Files

To link a DLL to a MEX-file, list the DLL's .lib file on the command line.

Versioning Build MEX-Files

The mex build script can build your MEX-file with a resource file that contains versioning and other essential information. The resource file is called mexversion.rc and resides in the extern\include directory. To support versioning, there are two new commands in the options files, RC_COMPILER and RC_LINKER, to provide the resource compiler and linker commands. It is assumed that

Compiling MEX-Files with the Microsoft® Visual C++® IDE

To build MEX-files with the Microsoft Visual C++ integrated development environment:

  1. Create a project and insert your MEX source files.

  2. Add mexversion.rc from the MATLAB include directory, matlab\extern\include, to the project.

  3. Create a .def file to export the MEX entry point. On the Project menu, click Add New Item and select Module-Definition File (.def). For example:

    LIBRARY MYFILE
    EXPORTS mexFunction          <-- for a C MEX-file
       or
    EXPORTS _MEXFUNCTION@16       <-- for a Fortran MEX-file
  4. On the Project menu, click Properties for the project to open the property pages.

  5. Under C/C++ General properties, add the MATLAB include directory, matlab\extern\include, as an additional include directory.

  6. Under C/C++ Preprocessor properties, add MATLAB_MEX_FILE as a preprocessor definition.

  7. Under Linker General properties, change the output file extension to .mexw32 if you are building for a 32–bit platform or .mexw64 if you are building for a 64–bit platform.

  8. Locate the .lib files for the compiler you are using under matlabroot\extern\lib\win32\microsoft or matlabroot\extern\lib\win64\microsoft. Under Linker Input properties, add libmx.lib, libmex.lib, and libmat.lib as additional dependencies.

  9. Under Linker Input properties, add the module definition (.def) file you created.

  10. Under Linker Debugging properties, if you intend to debug the MEX-file using the IDE, specify that the build should generate debugging information. For more information about debugging, see Debugging on the Microsoft® Windows® Platforms.

If you are using a compiler other than the Microsoft Visual C++ compiler, the process for building MEX files is similar to that described above. In step 4, locate the .lib files for the compiler you are using in a subdirectory of matlabroot\extern\lib\win32 or matlabroot\extern\lib\win64. For example, if you are using an Open Watcom C++ compiler, look in matlabroot\extern\lib\win32\watcom.


[1] UNIX is a registered trademark of The Open Group in the United States and other countries.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS