I would like to compile an Engine program using MSVC 6.0 or earlier and MATLAB 6.0 (R12) or earlier.
MATLAB: How to compile an Engine program using MSVC 6.0 or earlier and MATLAB 6.0 (R11) or earlier
compileengineideMATLABmsvc
Related Solutions
We are unable to offer complete technical support on the details of using any specific one of the large number of IDE environments our customers use. If you need detailed assistance with the particular settings needed to get your IDE environment to generate code that successfully compiles and runs with our products, we suggest you contact the manufacturer of your IDE to get either information or expert technical assistance in using it.
See the Related Solution for a working project file for MSVC 6.0 on Windows NT.
If you do not have MSVC 6.0, you will need to follow these steps to create a stand-alone C or C++ executable with the Microsoft Visual C/C++ 5.0 IDE and C/C++ Math Libraries, and the C/C++ Graphics Library:
1. Start up the Microsoft Visual C/C++ 5.0 IDE
2. Go to FILE and NEW. Click on Projects Tab. Select Win32 Console Application. In the Project Name field type: ex1. Create new workspace should be filled in. In the Platforms field, Win32 should also be checked. Click OK.
3. Go to the MAIN menu and click on Project, Add to Project, New. Click on Projects Tab. Select Win32 Static Library. In the Project Name field type in libmx. Add to current workspace should be filled in. Dependency of should be checked. In the listbox select ex1. In the Platforms field, Win32 should be checked. Click OK.
4. Highlight Workspace 'ex1': 2 project(s) and then right click. Select Add new project to workspace. Click on Projects Tab. Select Win32 Static Library. In the Project Name field type in libmcc. Add to current workspace should be filled in. Dependency of should be checked. In the listbox select ex1. In the Platforms field Win32 should be checked. Click OK.
5. Do step 4 for libmmfile and libmatlb. If you are building a project which requires the Graphics Library, you should also do this for sgl.
6. Highlight ex1 files. Right click and select Set as Active Project.
7. Highlight ex1 files. Right click and select New Folder. In the Name of the new folder field type in Sources. Click OK.
8. Do step 7 for libmatlb files, libmx files, libmcc files, and libmmfile files. Replace ex1 with the appropriate names. If you are building a project which requires the Graphics Library, you should also do this for the sgl files.
NOTE: You do not need libmcc files for MATLAB R12 or higher
9. Double click on ex1 files. Highlight Sources and then right click. Select Add files to folder. Go to $MATLAB/extern/examples/cppmath and select ex1.cpp. $MATLAB represents the root directory where MATLAB is installed.
10. Double click on libmx files. Highlight Sources and then right click. Select Add files to folder. Go to $MATLAB/extern/include and select libmx.def.
11. Do step 10 for libmatlb files, libmcc files, and libmmfile files. Replace libmx with the appropriate names. If you are building a project which requires the Graphics Library, you should also do this for the sgl files.
12. Double click on libmatlb files and double click on Sources. Right click on libmatlb.def and select Settings. This brings up a Project Settings window. Click on Custom Build Tab. In the Build Command(s) field enter:
lib /def:"c:\matlab\extern\include\libmatlb.def" /machine:ix86 /OUT:libmatlb.lib
c:\matlab represents the root directory where MATLAB is installed on your system.
In the Output file(s) field type: libmatlb.lib.
Click OK.
13. Do step 12 for libmx files, libmmfile files, and libmcc files. Replace libmatlb with the appropriate names. If you are building a project which requires the Graphics Library, you should also do this for the sgl files.
14. Highlight ex1 files and right click. Select Settings. Click on the C/C++ Tab. In the Category listbox select Code Generation. In the Use Runtime library listbox select Multithreaded DLL. Change the Category listbox to Preprocessor. Add to the Preprocessor definitions MSVC, MSWIND, IBMPC so:
WIN32,_DEBUG,_CONSOLE,_MBCS
changes to:
WIN32,_DEBUG,_CONSOLE,_MBCS,MSVC,MSWIND,IBMPC
Add to the Additional include directories field:
c:\matlab\extern\include\cpp;c:\matlab\extern\include;
c:\matlab\extern\include\cpp is only necessary if you are using the C++ Math Library.
Click on the Link Tab. Select in the Category listbox General. If you are creating C++ executables you will need to add libmatpm.lib to the Object/Library modules field. If you are creating C executables you do not need to add libmatpm. Therefore, for C++ applications, the Object/Library modules field would change from:
kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib
to:
c:\matlab\extern\lib\libmatpm.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib
Click OK.
15. Go to Build and Rebuild All.
16. Go to Build and Execute ex1.exe.
Here is how to generate a C-shared library with MATLAB Compiler 3.0 and how to use it in general in an integrated development environment (IDE), using Microsoft Visual Studio 6 as an example.
Here are the steps to create a C - shared library:
1. Compile your MATLAB files into a DLL (on Windows):
mcc -t -L C -W lib:mylib -T link:lib -h <MATLAB files> libmmfile.mlib
The -t option tells the MATLAB Compiler to translate the MATLAB code to the target language.
The -L option specifies the target language, which is chosen to be C.
The -W option tells the MATLAB Compiler to build a wrapper for libraries with the name specified after "lib:".
The -T option tellls the compiler what stage should be reached and for what intentions. Here we want to link our application together to build a shared library (i.e. DLL on a PC).
Specifying libmmfile.mlib tells the MATLAB Compiler to link against the MATLAB file Math routines that have already been precompiled into shared libraries when necessary.
This step will produce mylib.dll, mylib.lib, and mylib.h. You can add the
-g switch to produce a DLL suitable for debugging in MSVC (or your own IDE).
2. Add mylib.lib to your MSVC (or your own IDE) project
3. Make sure to call the initialization and termination routines from your
code before calling any of the MATLAB files that are compiled. You need to call:
mylibInitialize();
Afterwards, you should call the termination routine:
mylibTerminate();
All of the symbols in mylib.dll will also appear in mylib.h.
4. You can call the functions compiled from the MATLAB code by invoking mlfFoo(...), from your C code.
For example:
In MATLAB,
1. Write a MATLAB file function named foo and saved as foo.m that contains the following code:
function y = foo(x)y = x+1;
2. Use the MATLAB Compiler to compile foo.m into C code that can be included in a C shared library:
mcc -t -L C -W lib:foolib -T link:lib foo.m libmmfile.mlib
This will generate the following files:
foo.c
foo.h
foolib.c
foolib.h
foolib.exports
foolib.dll
foolib.exp
foolib.lib
foolib.mlib
In Microsoft Visual C/C++ 6.0
1. Start up the Microsoft Visual C/C++ 6.0 IDE
2. Go to FILE and NEW. Click on Projects Tab. Select Win32 Console Application. In the Project Name field type "test". "Create new workspace" should be filled in. In the "Platforms" field, "Win32" should also be checked. Click OK.
3. Select the File View tab and double click on test files. Select Source Files, select Project menu option, select Add to Project and then click on New. In the Files tab select C++ Source file, check Add to project and type "foowrap.c" in the File name field.
4. Enter the following code in the foowrap.c file:
#include "foolib.h"#include "matlab.h"#include <stdio.h>void main(void){ mxArray *x_ptr; mxArray *y_ptr; double *y; double ret; /* Create an mxArray to input into mlfFoo */ x_ptr = mxCreateDoubleScalar(1); /* Call the library initialization function */ foolibInitialize(); /* Call the implementation function */ y_ptr = mlfFoo(x_ptr); /* Call the library termination function */ foolibTerminate(); /* The return value from mlfFoo is an mxArray so we must extract the data from it */ y = mxGetPr(y_ptr); ret = *y; /* Print a double precision number*/ printf("The output of foo is %f\n",ret); mxDestroyArray(x_ptr); mxDestroyArray(y_ptr);}
5. Right click on test files and select Add Files to Project. Select:
foolib.lib
and the following files located in $MATLABROOT\extern\lib\win32\microsoft\msvc60:
libmatlb.lib
libmmfile.lib
libmx.lib
where $MATLABROOT is the MATLAB root directory on your machine, as returned by typing
matlabroot
at the MATLAB Command Prompt.
6. Highlight test files and right click. Select Settings. Click on the C/C++ Tab. In the Category listbox select Code Generation. In the Use Runtime library listbox select Multithreaded DLL. Change the Category listbox to Preprocessor. Add to the Preprocessor definitions MSVC, MSWIND, IBMPC so:
WIN32,_DEBUG,_CONSOLE,_MBCS
changes to:
WIN32,_DEBUG,_CONSOLE,_MBCS,MSVC,MSWIND,IBMPC
Add to the Additional include directories field:
$MATLABROOT\extern\include\cpp;$MATLAB\extern\include
Also add to the Additional include directories field the directory in which foolib.h is located.
Click OK
7. Go to Build and Rebuild All.
8. You should now have built test.exe.
9. You may also need to place foolib.dll in the same location as test.exe.
For information on how to call a MATLAB Compiler generated DLL from a Visual C++ environment, see the following Related Solution.
Related Question
- How to use a C++ shared library that was generated with MATLAB Compiler 4.0 (R14) with MSVC 6.0
- How to compile an Engine application that uses MATLAB 7.0 (R14) in Visual Studio .NET 2003
- How to compile matcreat.cpp demo using Microsoft Visual Studio Express 2010
- How to use Intel Fortran Composer XE 2011 12.0 and Microsoft Visual Studio 2010 to compile Fortran code into a MEX file for MATLAB 7.13 (R2011b)
- How to configure the IDE to compile MATLAB Engine applications
- How to compile a MATLAB Engine Fortran application using Microsoft Visual Studio 2008, Intel Visual Fortran 11.1 and MATLAB 7.10 (R2010a)
- How to debug a Fortran MEX file on Windows using Intel, Compaq, or Digial Visual Fortran compilers with Microsoft Visual Studio
Best Answer