Skip to Main Content Skip to Search
Home |   Select Country  Choose Country  |  Contact Us  |  Cart Store 
Create Account | Log In
Products & Services Industries Academia Support User Community Company

 

Product Support

1621 - What should I do before trying to compile?

There are a number of sample files shipped with MATLAB that are used to make sure the installation of MATLAB, the MATLAB Compiler, and the C/C++ Math and/or Graphics Libraries are correct. These example files can be found in the $MATLAB\extern\examples (where $MATLAB is the MATLAB root directory)directory. This directory, and all sub-directories, are not on the MATLAB path. When trying to use these files, you will have to CD into the appropriate directory. These files are referenced frequently in this solution, so ensure you are in the specified directory before trying to compile and run the files.

Before Compiling a MEX File

  1. Run the following command from the MATLAB command prompt:
    mex -setup

    When you run this command, a series of questions are asked regarding the location of the C or Fortran compiler you would like to use to compile your code. After answering these questions, a MEX options file is created that gives MATLAB all of the information it needs to use your compiler during compilation.

  2. Try compiling our sample MEX-file found in the $MATLAB\extern\examples\mex directory. The file to try to compile is called yprime.c.

    If you are using C, type the following at the MATLAB Command Prompt to compile the file:

    cd(matlabroot) cd extern\examples\mex mex yprime.c

    If you are using Fortran, type the following at the MATLAB Command Prompt:

    mex yprime.f yprimefg.f 

    This creates a MEX file that can be used at the command prompt like any M-file. If you now type

    yprime(1,1:4) 
    you should get the following output:

    ans=
    2.0000
    8.9685
    4.0000
    -1.0947

    If you do not get this result, or you receive error messages when trying to compile, add a -v flag to your compilation line. This command would look like this:

    mex -v yprime.c 
    This will produce a lot of output (v is for verbose) that shows the compiling and linking process. This may give some more information about why the compilation is failing.

Before Compiling an M-File into C/C++ Code

  1. Run the following command from the MATLAB command prompt:
    mbuild -setup 

    When you run this command, a series of questions will be asked regarding what libraries you have installed and which ones you want to link to during compilation. After answering these questions, you are ready to start compiling and linking your code.

  2. Verify that MBUILD is working correctly by compiling our example C or C++ file (called ex1.c or ex1.cpp) with the MATLAB Math Libraries. This file is located in the $MATLAB\extern\examples\cmath (for C Math Libraries) or $MATLAB\extern\examples\cppmath (for C++) directory. You have already indicated that you want to link to the MATLAB libraries during compilation, so to link and build your stand-alone executable, type the following command:

    mbuild ex1.c 
    (for C code)

    or
    mbuild ex1.cpp 
    (for C++ code)

    If you are in the correct directory, the result is a file called ex1.exe (on PC) or ex1 (on UNIX). This new file should be in your working directory. You can now run your executable by typing:

    !ex1 
    (from inside MATLAB)

    or
    ex1 
    (from your DOS or UNIX prompt)

    This will run your executable file and should result in:
    1     3     5 
    2     4     6 
     
    1.0000 + 7.0000i   4.0000 +10.0000i 
    2.0000 + 8.0000i   5.0000 +11.0000i 
    3.0000 + 9.0000i   6.0000 +12.0000i

    Again, if you do not get this result, or you receive error messages when trying to compile, add a -v flag to your compilation line.

  3. Verify that the compiler is working correctly by compiling and linking our example Hello World program. This file is located in $MATLAB\extern\examples\compiler. We use the -m flag to compile in order to result in a stand-alone C executable. In order to compile and link in one step, type the following at the MATLAB command prompt:
    mcc -m hello.m 
    (for a C executable)

    or
    mcc -p hello.m
    (for a C++ executable)

    If you are in the correct directory, the result is a file called hello.exe (on PC) or hello (on UNIX). This new file should be in your working directory. You can now run your executable. Try typing:

    !hello 
    (from inside MATLAB) or
    hello 
    (from your DOS or UNIX prompt)

    This will run your executable file and should result in a displayed message that looks like this:

    Hello, World 

    Just like using MEX and MBUILD, if you do not get this result, or you receive error messages when trying to compile, add a -v flag to your compilation line.

    For more information about different flags that can be used with the MCC command, see Technical Note 1622.

  4. Finally, if you have the Graphics Libraries installed and have indicated to link to them during compilation when you set up MBUILD, try compiling our sample graphics application found in $MATLAB\extern\examples\sgl. This file is called flames.m. To compile a graphics application, you must use the -B sgl (for C) or -B sglcpp (for C++) flag. type the following at the MATLAB command prompt after you have moved to the correct directory:
    mcc -B sgl flames.m 
    (for a C executable)

    or
    mcc -B sglcpp flames.m 
    (for a C++ executable)

    This creates an executable file in your working directory called flames.exe (on a PC) or flames (on a UNIX). You can run this executable by typing:

    !flames 
    (from inside MATLAB)

    or
    flames
    (from your DOS or UNIX prompt)

    This should result in a small figure that will show a short animation.

You are now ready to start compiling your own files. Remember when transferring your executables to another machine to include the required DLL files.

Contact support
E-mail this page
Print this page