|On this page…|
To create an engine application, you need to build with an options file and set the run-time library path. The following topics describe these general requirements. For platform-specific informations, see Building and Running Engine Applications on Windows Operating Systems or Building and Running Engine Applications on UNIX Operating Systems.
Use the mex function to compile and link engine applications. MATLAB® provides an options file containing compiler-specific flags that correspond to the general compile, prelink, and link steps required by your development tools. The name of the options file depends on your operating system and which compiler you use. For Windows® systems, see Engine Options Files on Windows. On UNIX® systems, the options file is engopts.sh in the matlabroot/bin folder.
The format of the build command is:
where fileName is the name of your C/C++ or Fortran source file, and optionsPath and optionsName make up the full file name of the options file.
Alternatively, copy the options file to your current working folder, and then enter a command like:
mex -f optionsName fileName
At run time, tell the operating system where the MATLAB API shared libraries reside by setting the run-time library Path environment variable. For instructions, see Setting Run-Time Library Path on Windows or Setting Run-Time Library Path on Linux and Macintosh.
If you have multiple versions of MATLAB installed on your system, the version you use to build your engine applications must be the first listed in your system Path environment variable. Otherwise, MATLAB displays Can't start MATLAB engine. For information about setting the Path variable, see Setting Run-Time Library Path on Windows or Setting Run-Time Library Path on Linux and Macintosh.
The following topics describe what you need to know to create engine applications. For an example, see Windows Engine Example engwindemo.
The name of the options file is *engmatopts.bat, where * is a string representing the compiler name and version. To identify the options files on your system, type:
The Name and Version properties of a mex.CompilerConfiguration object can help you select an options file. Type:
cc = mex.getCompilerConfigurations('any','supported');
For example, for a cc object with the following properties, chose the msvc90engmatopts.bat options file.
Properties: Name: 'Microsoft Visual C++ 2008' Manufacturer: 'Microsoft' Language: 'C++' Version: '9.0' Location: 'c:\Program Files (x86)\Microsoft Visual Studio 9.0' Details: [1x1 mex.CompilerConfigurationDetails]
Set the Path environment variable to the path string returned by the following MATLAB command:
To set an environment variable on Windows XP, select Start > Settings > Control Panel > System. The System Properties dialog box appears. Click the Advanced tab, and then click the Environment Variables button.
In the System variables panel scroll down until you find the Path variable. Click this variable to highlight it, and then click the Edit button to open the Edit System Variable dialog box. At the end of the path string, enter a semicolon. Then, enter the path string that MATLAB returns after evaluating the expression shown above. Click OK in the Edit System Variable dialog box, and in all remaining dialog boxes.
To run the engine application on a Windows operating system, you need to register MATLAB as a COM server. Do this for every session, to ensure that the current version of MATLAB is the registered version. If you run older versions, the registered version could change. If there is a mismatch of version numbers, MATLAB displays Can't start MATLAB engine.
To manually register MATLAB as a server, type:
cd(fullfile(matlabroot,'bin',computer('arch'))) !matlab /regserver
Close the MATLAB window that appears.
To verify the build process on your computer, use the C example engwindemo.c.
Copy the file to your current working folder:
copyfile(fullfile(matlabroot,... 'extern','examples','eng_mat','engwindemo.c'),... '.', 'f');
Build the executable file. If you are using a Microsoft® Visual C++® compiler, select the appropriate options file, as described in Engine Options Files on Windows. If you selected the Lcc compiler, type:
mex('-v', '-f', fullfile(matlabroot,... 'bin','win32','mexopts','lccengmatopts.bat'),... 'engwindemo.c');
Verify that the build worked by looking in your current working folder for the engwindemo.exe file:
Run the example from MATLAB:
For more information about the engwindemo application, see Call MATLAB Functions from C Applications.
The following topics describe what you need to know to create engine applications. For an example, see UNIX Engine Example engdemo.
On UNIX systems, the options file is:
and the path is:
The UNIX command you use and the value you provide to set the run-time library path depend on your shell and system architecture. The following table lists the name of the environment variable, envvar, and the values, pathspec, to assign to it.
64-bit Apple Macintosh (Intel®)
setenv envvar pathspec
envvar = pathspec:envvar export envvar
To verify the build process on your computer, use the C example engdemo.c or the C++ example engdemo.cpp.
Copy one of the programs, for example, engdemo.c, to your current working folder:
copyfile(fullfile(matlabroot,... 'extern','examples','eng_mat','engdemo.c'),... '.', 'f');
Build the executable file:
mex('-v', '-f', fullfile(matlabroot,... 'bin','engopts.sh'),... 'engdemo.c');
Verify that the build worked by looking in your current working folder for the engdemo application:
Run the example in MATLAB:
For more information about the engdemo applications, see Call MATLAB Functions from C Applications.