Changes between Version 1 and Version 2 of howto/MEX_Compile


Ignore:
Timestamp:
Aug 26, 2013, 10:07:11 AM (11 years ago)
Author:
welsh
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • howto/MEX_Compile

    v1 v2  
    1818=== Setting up the Matlab environment for compiling MEX ===
    1919
    20 TBD
     20To setup MATLAB to compile MEX, you first need to run
     21{{{
     22mex -setup
     23}}}
     24This will invoke the MATLAB utility to setup the default compiler:
     25{{{
     26Welcome to mex -setup.  This utility will help you set up 
     27a default compiler.  For a list of supported compilers, see 
     28http://www.mathworks.com/support/compilers/R2011a/win64.html
     29 
     30Please choose your compiler for building MEX-files:
     31 
     32Would you like mex to locate installed compilers [y]/n?
     33}}}
     34Select "Yes" and MATLAB should locate the compiler you installed above.
     35{{{
     36 
     37Select a compiler:
     38[1] Microsoft Visual C++ 2010 Express in C:\Program Files (x86)\Microsoft Visual Studio 10.0
     39 
     40[0] None
     41 
     42Compiler:
     43}}}
     44Select "Microsoft Visual C++ 2010 Express" as the compiler to use.  Then verify your selection:
     45{{{
     46Please verify your choices:
     47 
     48Compiler: Microsoft Visual C++ 2010 Express 
     49Location: C:\Program Files (x86)\Microsoft Visual Studio 10.0
     50 
     51Are these correct [y]/n?
     52}}}
     53Select "Yes" and you should see a couple of warnings:
     54{{{
     55***************************************************************************
     56  Warning: MEX-files generated using Microsoft Visual C++ 2010 require
     57           that Microsoft Visual Studio 2010 run-time libraries be 
     58           available on the computer they are run on.
     59           If you plan to redistribute your MEX-files to other MATLAB
     60           users, be sure that they have the run-time libraries.
     61***************************************************************************
     62 
     63Trying to update options file: C:\Users\<username>\AppData\Roaming\MathWorks\MATLAB\R2011a\mexopts.bat
     64From template:              C:\MATLAB\R2011a\bin\win64\mexopts\msvc100freeopts.bat
     65 
     66Done . . .
     67 
     68**************************************************************************
     69  Warning: The MATLAB C and Fortran API has changed to support MATLAB
     70           variables with more than 2^32-1 elements.  In the near future
     71           you will be required to update your code to utilize the new
     72           API. You can find more information about this at:
     73           http://www.mathworks.com/support/solutions/en/data/1-5C27B9/?solution=1-5C27B9
     74           Building with the -largeArrayDims option enables the new API.
     75**************************************************************************
     76}}}
     77
     78If you have any issues with compiling MEX, you can look at the compiler options by running:
     79{{{
     80cc = mex.getCompilerConfigurations()
     81}}}
     82by exploring the "Details" field, you can see all of the Compiler and Linker flags.  By looking at {{{mex -help}}}, you can see how to override the default compiler and linker options.
     83
     84
    2185
    2286=== Compiling MEX ===
    2387
    24 TBD
     88To see all the options when compiling MEX, please run:
     89{{{
     90mex -help
     91}}}
     92Some parameters that are commonly used (ie you can see them as part of the compile command for [source:/ResearchApps/PHY/WARPLAB/WARPLab7/M_Code_Reference/mex/wl_mex_udp_transport.c wl_mex_udp_transport.c]) are:
     93  1. This provides debug information that allows you to debug your MEX C / C++ code within Microsoft Visual C++ Express 2010. 
     94{{{
     95       -g
     96           Create a MEX-file containing additional symbolic information for
     97           use in debugging. This option disables MEX's default behavior of
     98           optimizing built object code (see the -O option).
     99}}}
     100  1. This optimizes your code, even if the debug options are turned on.  Early in your debug process, you should leave this option out so that you can better understand what the code is doing.
     101{{{
     102       -O
     103           Optimize the object code. Optimization is enabled by default and
     104           by including this option on the command line. If the -g option
     105           appears without the -O option, optimization is disabled.
     106}}}
     107  1. If you need any external libraries, like the [http://msdn.microsoft.com/en-us/library/windows/desktop/ms738545%28v=vs.85%29.aspx Winsock Library], then you need to specify the object library as part of the compile.
     108{{{
     109       -l<name>
     110           Link with object library. On Windows, name expands to
     111           "<name>.lib" or "lib<name>.lib" and on UNIX, to
     112           "lib<name>.LIBEXT", where LIBEXT is platform dependent.  Do not
     113           add a space after this switch.
     114}}}
    25115
     116One example of a compile command used for the MEX UDP Transport within WARPLab is:
     117{{{
     118mex -g -O wl_mex_udp_transport.c -lwsock32 -lKernel32
     119}}}
     120
     121
     122=== Debugging MEX in MATLAB ===
     123
     124To debug the MEX code, first open Microsoft Visual C++ Express 2010 that you installed above.
     125
     126Next, open the MEX C file within Microsoft Visual C++ Express 2010:
     127{{{
     128"File"->"Open"->"File" (Ctrl+O)
     129}}}
     130For example, you can open [source:/ResearchApps/PHY/WARPLAB/WARPLab7/M_Code_Reference/mex/wl_mex_udp_transport.c wl_mex_udp_transport.c] from the WARPLab release.
     131
     132[[Image(MEX_Debug_Open_File.png, 600px)]]
     133
     134Once the file is open, you need to have MATLAB open and ready to run your MEX code.  Now, select:
     135{{{
     136"Tools" -> "Attach to Process ..." (Ctrl+Alt+P)
     137}}}
     138If you don't see this menu option, please select
     139{{{
     140"Tools" -> "Settings" -> "Expert Settings"
     141}}}
     142After you select "Attach to Process" a dialog will appear that will allow you to select '''MATLAB.exe''' as the process.  Once Visual C++ Express 2010 attaches to a process, the screen should change to the "Debug" view:
     143
     144[[Image(MEX_Debug_Attach.png, 600px)]]
     145
     146Once you attach to the process, you should then be able to debug your code (ie set breakpoints, etc) using the standard Visual C++ Express 2010 methods.  For example, once you have set a breakpoint, then you can run your code within MATLAB and have the program halt at the breakpoint to see the various variables and other watchpoints:
     147
     148[[Image(MEX_Debug_Breakpoint.png, 600px)]]
     149
     150
     151
     152
     153
     154