External Interfaces/API | ![]() ![]() |
Testing Your Configuration on Windows
Before you can create MEX-files on the Windows platform, you must configure the default options file, mexopts.bat
, for your compiler. The switch, setup
, provides an easy way for you to configure the default options file. To configure or change the options file at anytime, run
mex -setup
from either the MATLAB or DOS command prompt.
Selecting a Compiler
MATLAB includes a C compiler, Lcc, that you can use to create C MEX-files. The mex
script will use the Lcc compiler automatically if you do not have a C or C++ compiler of your own already installed on your system and you try to compile a C MEX-file. Naturally, if you need to compile Fortran programs, you must supply your own supported Fortran compiler.
The mex
script uses the filename extension to determine the type of compiler to use for creating your MEX-files. For example,
mex test1.f
would use your Fortran compiler and
mex test2.c
On Systems without a Compiler. If you do not have your own C or C++ compiler on your system, the mex
utility automatically configures itself for the included Lcc compiler. So, to create a C MEX-file on these systems, you can simply enter
mex filename.c
This simple method of creating MEX-files works for the majority of users.
If using the included Lcc compiler satisfies your needs, you can skip ahead in this section to Building the MEX-File on Windows.
On Systems with a Compiler. On systems where there is a C, C++, or Fortran compiler, you can select which compiler you want to use. Once you choose your compiler, that compiler becomes your default compiler and you no longer have to select one when you compile MEX-files. To select a compiler or change to existing default compiler, use mex -setup
.
This example shows the process of setting your default compiler to the Microsoft Visual C++ Version 6.0 compiler.
mex -setup Please choose your compiler for building external interface (MEX) files. Would you like mex to locate installed compilers [y]/n? n Select a compiler: [1] Borland C++Builder version 5.0 [2] Borland C++Builder version 4.0 [3] Borland C++Builder version 3.0 [4] Borland C/C++ version 5.02 [5] Borland C/C++ version 5.0 [6] Compaq Visual Fortran version 6.1 [7] Digital Visual Fortran version 5.0 [8] Lcc C version 2.4 [9] Microsoft Visual C/C++ version 6.0 [10] Microsoft Visual C/C++ version 5.0 [11] WATCOM C/C++ version 11 [12] WATCOM C/C++ version 10.6 [0] None Compiler: 9 Your machine has a Microsoft Visual C/C++ compiler located at D:\Applications\Microsoft Visual Studio. Do you want to use this compiler [y]/n? y Please verify your choices: Compiler: Microsoft Visual C/C++ 6.0 Location: D:\Applications\Microsoft Visual Studio Are these correct?([y]/n): y The default options file: "C:\WINNT\Profiles\username\ApplicationData\MathWorks\MATLAB\R12 \mexopts.bat" is being updated from ...
If the specified compiler cannot be located, you are given the message:
The default location for compiler-name is directory-name, but that directory does not exist on this machine. Use directory-name anyway [y]/n?
Using the setup
option sets your default compiler so that the new compiler is used everytime you use the mex
script.
Building the MEX-File on Windows
There is example C source code, yprime.c
, and its Fortran counterpart, yprimef.f
and yprimefg.f
, included in the <matlab>\extern\examples\mex
directory, where <matlab>
represents the top-level directory where MATLAB is installed on your system.
To compile and link the example source file on Windows, at the MATLAB prompt, type
cd([matlabroot '\extern\examples\mex'])
mex yprime.c
This should create the MEX-file called yprime
with the .DLL
extension, which corresponds to the Windows platform.
You can now call yprime
as if it were an M-function.
yprime(1,1:4) ans = 2.0000 8.9685 4.0000 -1.0947
To try the Fortran version of the sample program with your Fortran compiler, switch to your Fortran compiler using mex -setup
. Then, at the MATLAB prompt, type
cd([matlabroot '\extern\examples\mex'])
mex yprimef.f yprimefg.f
In addition to running the mex
script from the MATLAB prompt, you can also run the script from the system prompt.
![]() | Testing Your Configuration on UNIX | Specifying an Options File | ![]() |