Targeting Mono in Visual Studio 2012
These steps are known good on my Windows 8 machine, with Visual Studio 2012 w/ Update 1 and Mono 2.10.9.
- Install Mono for Windows, from http://www.go-mono.com/mono-downloads/download.html
Choose a decent path, which for me was
C:\Program Files (x86)\Mono-2.10.9
- Load an elevated administrative Command Prompt (Top tip: On Windows 8, hit WinKey+X then choose “Command Prompt (Admin)“)
- From this Command Prompt, execute the following commands (in order):
$ cd "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile"
$ mklink /d Mono "C:\Program Files (x86)\Mono-2.10.9\lib\mono\4.0"
$ cd Mono
$ mkdir RedistList
$ cd RedistList
$ notepad FrameworkList.xml
- Notepad will start and ask about creating a new file, choose Yes.
- Now paste in this text and Save the file:
<?xml version="1.0" encoding="UTF-8"?>
<FileList ToolsVersion="4.0" RuntimeVersion="4.0" Name="Mono 2.10.9 Profile" Redist="Mono_2.10.9">
- From the same Command Prompt, type:
- In the Registry Editor, navigate to:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319\SKUs\and create a new Key folder called
- Now navigate to
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v4.0.30319\SKUs\and create the same Key folder again here (this step is only necessary for x64 machines, but since all software developers use those then you’ll probably need to do it!)
- Now load up VS2012 and a project. Goto the Project Properties (Alt+Enter whilst having it selected on Solution Explorer) .
- Choose the Application tab on the left and look for the “Target framework” drop-down list.
- On this list you should see an entry called “Mono 2.10.9 Profile”.
- Select it, and Visual Studio should then convert your project to target the Mono framework. You’ll notice that it will re-reference all your various System assemblies and if you study the filenames they will point to the Mono ones that were created during Step #3.
Note: I was scratching my head at first as I kept getting an error from Visual Studio saying:
This application requires one of the following versions of the .NET Framework:
Do you want to install this .NET Framework version now?
Turns out that even on a x64 machine you MUST add both Registry key SKUs (see Steps #7 and #8). It is not enough to just add the Wow6432Node key, you must add the other as well. I can only assume this is because VS2012 is a 32-bit application. But maybe it’s also affected by whether you’re compiling to Any CPU, x64 or x86… who knows. It doesn’t really matter as long as this fixes it, which it does!
Building and executing your first program
Now that your development environment is nicely setup, you can proceed and build your first program.
The Mono equivalent of MSBuild is called XBuild (not sure why they didn’t call it MBuild or something!). You can build your .csproj by doing the following:
- Load the Mono Command Prompt (it will be on your Start Menu/Screen, just search for “mono”).
- Change directory to your project folder.
- Execute the following command to build your project using the Mono compiler:
$ xbuild /p:TargetFrameworkProfile=""
Note: You must specify the blank
TargetFrameworkProfileparameter as otherwise the compiler will issue warnings along the lines of:
Unable to find framework corresponding to the target framework moniker ‘.NETFramework,Version=v4.0,Profile=Mono’. Framework assembly references will be resolved from the GAC, which might not be the intended behavior.
- Hopefully you’ll not have any errors from the build…
- Now you can run your program using the Mono runtime, to do this:
$ mono --gc=sgen "bin\Debug\helloworld.exe"
Note: You'll definitely want to use the "Sgen" garbage collector (hence the parameter) as the default one in Mono is unbearably slow.
- You can do a quick “smoke test” to verify everything is in order with both your compilation and execution. Have your program execute something like:
… and this should print out a path similar to:
I’ve no idea why it prints it out using 8.3 filename format, but there you go! You’ll notice that if you run your program outside of the Mono runtime then it will pick up the Microsoft CLR version from the GAC.