Compilation, linking, and running of a C# program

[ Source code | Bytecode | Compilation | Linking | Running |
  1. Source code

    The collection of clasess/structs in the C#-language which constitutes a program is called the source code. The source code is typically contained in one or several text files with extension .cs such as hello.cs. Apart from class definitions the source code can also contain certain directives for the compiler, like using.

  2. Bytecode

    The bytecode (which in the C# case is CIL) is the translation of the source-code into the instructions for an abstract processor realised by a runtime-system such as mono.

    A ready-to-exectue bytecode (with the "Main" function and all other necessary functions) is called executable and is typically contained in a binary file with extendion ".exe".

    A bytecode without the "Main" function is typically contained in a binary file with extension ".dll" and is called "library".

  3. Compilation

    The process of translation from C#-language to bytecode is called compilation and is typically done by a C#-compiler such as "mcs".

    A source code without the "Main" function is typically compiled into a bytecode library for later linking. For example, the command

    mcs -target:library -out:cmath.dll cmath.cs complex.cs
    
    takes the source code from the two files, cmath.cs and complex.cs, compiles them, and places the resulting bytecode library into the file cmath.dll.

    A source code with the "Main" function and all other necessary functions can be directly compiled into the executable bytecode (however, in practice you almost always use some libraries). For example, if the file hello.cs contains a whole program with the Main method, then the command

    msc hello.cs -target:exe -out:hello.exe
    
    compiles the source code and places the resulting executable bytecode and into the file hello.exe.

  4. Linking

    The functions from a bytecode library can be called in any source code if one links (with the compiler option "-reference") the library during compilation of that code. For example, if the source code in the file main.cs contains the Main function and calls functions from the "cmath.dll" library, the command

    mcs main.cs -reference:cmath.dll -target:exe -out:main.exe
    
  5. compiles "main.cs", links it with the function from the "cmath.dll" library, and places the resulting executable bytecode into the file "main.exe".

  6. Running

    The executable file "filename.exe" can be executed by the mono runtime system with the command

    mono filename.exe