Tapping the Mono C# compiler (mcs) [cont'd]

Regarding my former article now I’d like to give you a continued HowTo for tapping the Mono C# compiler with support for generics, nullable types and friend assemblies. Compared to the mcs tool (.NET 1.1 target runtime) you have to compile the gmcs fork (.NET 2.0 target runtime) of the mono compiler project.

As shown in my previous post first of all you must run the jay parser generator. But now please use the cs-parser.jay input file in the gmcs directory. Again jay will create the output file cs-parser.cs.

In the first step create a new Visual Studio solution and choose C# Class Library as project type. Afterwards add all *.cs files of the following directories to your project:

+ MONO-SRC\mcs\mcs
+ MONO-SRC\mcs\gmcs

Preferably you can put the whole mono stuff in a subdirectory of your solution and import the files listed above as links. Don’t forget to exclude the following two files because they are already present:

- MONO-SRC\mcs\mcs\AssemblyInfo.cs
- MONO-SRC\mcs\mcs\generics.cs

Finally please also add all files of …

+ MONO-SRC\class\Mono.CompilerServices.SymbolWriter

… as soon as the CryptoConvert.cs file of:

+ MONO-SRC\class\Mono.Security.Cryptography

In the second step define the following three Conditional Compilation Symbols in your Project Properties via the Build pane:

  • GMCS_SOURCE
  • MS_COMPATIBLE
  • NET_2_0

From now on you can compile gmcs. Well, there still exists a few warnings but I think you can ignore that till anytime gmcs becomes the default mono compiler :)

2 Responses to “Tapping the Mono C# compiler (mcs) [cont'd]”


  • Sorry for my late answer :( I was on vacation for a week 8)

    Unfortunately I’ve never tested compiling code with generics in gmcs ’cause I need only the C# parser of the Mono project. In my scenario I compiled gmcs as a Class Library which is in turn integrated in my application (a Static Source Code Analyzer) – this works without problems!

    Hmm. Does your gmcs use the proper references or assemblies e.g. for the type List?

  • When I follow these steps, the resulting gmcs doesn’t seem to know how to do generics. Are you getting results like the ones below?

    I tried compiling this:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    
    using System;
    using System.Collections.Generic;
    using System.Text;
     
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                List foo = new List();
                foo.Add("Hello");
                foo.Add("World");
     
                foreach (string s in foo)
                {
                    Console.WriteLine(s);
                }
            }
        }
    }

    And I get this:

    Program.cs(12,17): error CS1502: The best overloaded method match for
    `System.Collections.Generic.List.Add(T)' has some invalid arguments
    Program.cs(12,17): error CS1503: Argument 1: Cannot convert from `string' to `T'
    Program.cs(13,17): error CS1502: The best overloaded method match for
    `System.Collections.Generic.List.Add(T)' has some invalid arguments
    Program.cs(13,17): error CS1503: Argument 1: Cannot convert from `string' to `T'
    Program.cs(15,13): error CS0030: Cannot convert type
    `System.Collections.Generic.List.Enumerator.Current' to `string'
    Compilation failed: 5 error(s), 0 warnings

Leave a Reply