20 | | TBD |
| 20 | To setup MATLAB to compile MEX, you first need to run |
| 21 | {{{ |
| 22 | mex -setup |
| 23 | }}} |
| 24 | This will invoke the MATLAB utility to setup the default compiler: |
| 25 | {{{ |
| 26 | Welcome to mex -setup. This utility will help you set up |
| 27 | a default compiler. For a list of supported compilers, see |
| 28 | http://www.mathworks.com/support/compilers/R2011a/win64.html |
| 29 | |
| 30 | Please choose your compiler for building MEX-files: |
| 31 | |
| 32 | Would you like mex to locate installed compilers [y]/n? |
| 33 | }}} |
| 34 | Select "Yes" and MATLAB should locate the compiler you installed above. |
| 35 | {{{ |
| 36 | |
| 37 | Select a compiler: |
| 38 | [1] Microsoft Visual C++ 2010 Express in C:\Program Files (x86)\Microsoft Visual Studio 10.0 |
| 39 | |
| 40 | [0] None |
| 41 | |
| 42 | Compiler: |
| 43 | }}} |
| 44 | Select "Microsoft Visual C++ 2010 Express" as the compiler to use. Then verify your selection: |
| 45 | {{{ |
| 46 | Please verify your choices: |
| 47 | |
| 48 | Compiler: Microsoft Visual C++ 2010 Express |
| 49 | Location: C:\Program Files (x86)\Microsoft Visual Studio 10.0 |
| 50 | |
| 51 | Are these correct [y]/n? |
| 52 | }}} |
| 53 | Select "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 | |
| 63 | Trying to update options file: C:\Users\<username>\AppData\Roaming\MathWorks\MATLAB\R2011a\mexopts.bat |
| 64 | From template: C:\MATLAB\R2011a\bin\win64\mexopts\msvc100freeopts.bat |
| 65 | |
| 66 | Done . . . |
| 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 | |
| 78 | If you have any issues with compiling MEX, you can look at the compiler options by running: |
| 79 | {{{ |
| 80 | cc = mex.getCompilerConfigurations() |
| 81 | }}} |
| 82 | by 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 | |
24 | | TBD |
| 88 | To see all the options when compiling MEX, please run: |
| 89 | {{{ |
| 90 | mex -help |
| 91 | }}} |
| 92 | Some 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 | }}} |
| 116 | One example of a compile command used for the MEX UDP Transport within WARPLab is: |
| 117 | {{{ |
| 118 | mex -g -O wl_mex_udp_transport.c -lwsock32 -lKernel32 |
| 119 | }}} |
| 120 | |
| 121 | |
| 122 | === Debugging MEX in MATLAB === |
| 123 | |
| 124 | To debug the MEX code, first open Microsoft Visual C++ Express 2010 that you installed above. |
| 125 | |
| 126 | Next, open the MEX C file within Microsoft Visual C++ Express 2010: |
| 127 | {{{ |
| 128 | "File"->"Open"->"File" (Ctrl+O) |
| 129 | }}} |
| 130 | For 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 | |
| 134 | Once 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 | }}} |
| 138 | If you don't see this menu option, please select |
| 139 | {{{ |
| 140 | "Tools" -> "Settings" -> "Expert Settings" |
| 141 | }}} |
| 142 | After 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 | |
| 146 | Once 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 | |