9.7. Command Line Execution

C# shields you from the differences between operating systems with its File, Path, and Directory classes.

If you leave Xamarin Studio and go to the command line as described in Command Line Introduction, then you are exposed to the differences between the operating systems. Look over that section.

Thus far we have let Xamarin Studio hide what actually is happening when you execute a program. The natural environment for the text-based programs that we are writing is the command line. We need to get outside of Xamarin Studio.

  1. To show off the transition, first build or run the addition1 example project from inside Xamarin Studio.

  2. Open a terminal on a Mac or a Mono Command Prompt console in Windows.

  3. Following the Command Line Introduction, change the current directory to the addition1 example project directory.

  4. Then change to the subdirectory bin and then the subdirectory Debug.

  5. Enter the command to list the directory (dir in Windows; ls on a Mac).

  6. You should see addition1.exe. This is the compiled program created by Xamarin Studio. Enter the command

    mono addition1.exe
    

    This should run your program. Note that when you complete it, the window does not disappear! You keep that history. Keep this terminal/console window open until the end of the chapter.

  7. Windows only: On Windows, Xamarin Studio creates a regular Windows executable file. For consistency you can use the command above, but you no longer need Mono. You can just enter the command addition1.exe or the shorter addition1.

9.7.1. MCS: Compiling

Continue with the same terminal/console window. Let us now consider creating an executable program for addition1.cs, directly, without using Xamarin Studio:

  1. Enter the command cd .. and then repeat: cd .., to go up to the project directory.

  2. Print a listing of the directory. You should see addition1.cs but not addition1.exe.

  3. Try the command

    mono addition1.exe
    

    You should get an error message, because addition1.exe is not in the current directory.

  4. Enter the command

    mcs addition1.cs
    

    This is the Mono system compiler, building from the source code.

  5. Print a listing of the directory. You should see now addition1.exe, created by the compiler.

  6. Try the command again:

    mono addition1.exe
    

Now try a program that had multiple files. The project version addition3 uses the library class UIF. Continue with the same terminal/console window:

  1. Enter the commands:

    cd ../addition3
    mcs addition3.cs
    

    You should get an error about missing the UIF class. The mcs program does not know about the information Xamarin Studio keeps in its references.

  2. Extend the command:

    mcs addition3.cs ../ui/uif.cs
    

    That should work, now referring to both needed files.

  3. Enter the command

    mono addition3.exe
    
  4. Now let us try a project where we read a file. Enter commands

    cd ../sum_file
    mcs sum_file.cs
    mono sum_file.exe
    

    We ran this program earlier through Xamarin Studio. Recall that that entering the file name numbers.txt failed, and to refer to the right place for the numbers.txt file, we needed to use ..\..\numbers.txt or ../../numbers.txt. This time just enter numbers.txt. The program should work, giving the answer 16.

By default mcs and mono read from and write to the current directory of the terminal/console. In the situation above, sum_file.cs and numbers.txt were in the project directory, which is the current directory. Then sum_file.exe was written to and run from the same directory.

This is unlike the Xamarin Studio default, where the current directory for execution is not the project directory.

Under the hood, Xamarin Studio uses mcs also, with a bunch of further options in the parameters, changing the execution directory and also arranging for better debugging information when you get a runtime error.

Xamarin Studio keeps track of all of the parts of your projects, and recompiles only as needed. There are also command-line tools that manage multi-file projects neatly, remembering the parts, and compiling only as necessary. One example is NAnt, which comes with Mono.