As it stands, Visual Studio does not allow you to explicitly target a framework other than the .NET framework. I cover that in this post, so if you are curious about that, then you may want to go read that first to understand where this post is coming from. But thankfully, Jason left me a comment that pointed me to this post by Jonathan Pobst. It was a big help in researching this post and basically set me on the right path to getting things to where I wanted them.
The goal was to get some Mono project templates working with Mono assemblies and the Mono compiler in Visual Studio. The trick was to do it in a way that would be able to survive upgrades of Mono installations. I think I pulled it off.
The end result is a series of project templates that you can put in your \My Documents\Visual Studio 2008\Templates\ProjectTemplates\Visual C#\Mono directory that will give you the following New Project options:
Notice the Mono option on the left hand side of the dialog and the Mono project types in the right hand side. Yep, that’s the little Mono monkey there too.
In order to use these, you are going to have to do a few things:
1.) Download and install Mono. I installed the current release (2.2) to C:\Mono\2.2
2.) Add a MonoPath environment variable and have it point to C:\Mono\2.2\bin
3.) Add a MonoLibPath environment variable and have it point to C:\Mono\2.2\lib\mono\2.0
4.) Reboot so the environment variables will be recognized
5.) Grab the download at the end of this post and unzip it. In it, you will find 3 project templates and 1 file called Mono.CSharp.targets.
6.) Place the 3 project template files (.zip) in the \My Documents\Visual Studio 2008\Templates\ProjectTemplates\Visual C#\Mono directory. You’ll have to create the Mono directory.
7.) Now, grab the Mono.CSharp.targets file and place it in the C:\Mono\build directory. Again, you will have to create the build directory.
8.) Now, fire up Visual Studio and bring up the New Project dialog. You should see something similar to the New Project Dialog above. (Note: If you don’t see the Mono option in the left side of the dialog then you may need to add the Mono directory to the \My Documents\Visual Studio 2008\Templates\ItemTemplates\VisualC#\ directory as well – see here – otherwise, you may only see them at the top level C#.)
9.) Now, go ahead and select one. You will get prompted with the following prompt from Visual Studio. Make sure you select the Load Project Normally option.
That should about do it. You will now be creating Visual Studio projects that reference the MonoPath and the MonoLibPath, so as you upgrade and wish to move from version to version, all you should have to do it change your environment variables and you will be compiling against the latest version of Mono and using the Mono compiler. (Presumably something like C:\Mono\2.4)
Now, some may point out that Mono is binary compatible with .NET so what’s all the fuss? The answer is that they are in different stages of development, so if you target the .NET framework and then try to run on Mono, you may end up with some gotchya’s. For example, a class or method call that is not yet implemented in Mono. Whereas if you target Mono, then you can be pretty assured, when referencing a System.*.dll it will be implemented in .NET. Also, if you use any of the Mono.*.dll assemblies in your projects, you’ll can just drag them around like any other referenced assembly. There are about 20 Mono.*.dll assemblies, so there is bound to be some goodies in there.
If you already have Mono installed somewhere, then you should be able to point your environment variables to the relative locations of your installation and you should still be good to go.
While writing this, I was listening to "Jane Says" by Jane’s Addiction
February 17, 2009 at 10:42 pm
Very awesome!
We actually write registry keys that point to the most recent Mono installation path at:
HKLM\Software\Novell\Mono
Would it be possible to have this just use them instead of setting the MonoPath/MonoLibPath env vars? You could probably still use them if you want to override the registry one, but it would reduce the effort needed to set this up for 99% of users.
February 18, 2009 at 3:40 am
[...] Getting Mono and Visual Studio to play nice together – Chris Cyvas shares the results of his exploration into getting Visual Studio to work with Mono templates, the mono compiler and to continue working after upgrading mono. [...]
February 18, 2009 at 7:12 am
@jpobst – Thank you for the suggestion! It looks as though we may be able to do that! Let me try it out this evening and we’ll see how it shakes out.
For anyone interested, here is what I will be using:
http://msdn.microsoft.com/en-us/library/bb383868.aspx
If anyone tries it out before I get to it, please leave a comment on whether it works or not.
March 12, 2009 at 6:37 am
[...] :: Part 2 Posted by chriscyvas under Coding, Mono, Open Source, dashCommerce In my last post, I profiled what you would need to do to get Visual Studio to use the Mono compiler for compiling [...]
March 17, 2009 at 2:33 pm
[...] to be restarted after saving the changes. I used the zipped project template files from this Chis Cyva Blog post about using Mono with Visual Studio 2008. The Mono project templates won’t be needed to run [...]
March 27, 2009 at 6:37 am
This is great, since I love VS, but would also like to be able to develop for Linux hosting, etc.
I noticed an incompatibility between the download and your blog entry, though. You can’t create a project because of this line in the .csprj files:
I think the “..\..” shouldn’t be there if we follow your folder structure in the blog. Unfortunately, I don’t see how to change this. I edited the files and rezipped it but it doesn’t show up. Is there some special way you have to zip the folder for VS to understand it?
March 27, 2009 at 6:38 am
Seems the HTMLesque tags caused my last post to drop the line in the proj file:
Import Project=”$(MonoPath)\..\..\build\Mono.CSharp.targets”
March 27, 2009 at 7:05 am
This is turning into spam, but to build I also had to change ‘ToolPath’ in the Mono.CSharp.targets file to add “\bin\”, since when you try to compile with Mono installed in C:\Mono\2.2 you get an error that it can’t find gmcs.bat (since it was looking simply in $(MonoPath):
ToolPath=”$(MonoPath)\bin\”
I can now compile, and it seems to work (though it complains that there is no debug information in the web app … I’m sure it’s just an obvious config issue, though).
Thanks a lot for this. I’ll have to see if I can bring my dashCommerce solution over to a new Mono project now, since Linux hosting is often significantly cheaper.
March 27, 2009 at 4:41 pm
My MonoPath is C:\Mono\2.2\bin and my MonoLibPath is C:\Mono\2.2\lib\mono\2.0
I have the ..\.. stuff in my project files and they are ok. Are you sure you put the Mono.CSharp.targets in C:\Mono\build?
April 3, 2009 at 1:51 pm
I do all the steps and i get
implement type compare for 1b error , when i run my project
Some idea about the error ?
April 3, 2009 at 2:09 pm
Yup – check out Part II
April 29, 2009 at 9:51 am
Would be nicer if you gave me a setup that I could install than letting us set the environment variables manually
May 14, 2009 at 8:22 pm
Thanks for this post! Got me up and running in under 5.
May 20, 2009 at 12:56 am
Thanks for info guys.
Anyone knows how to integrate mono with VS 2005.
I tried it myself.
But got stuck.
I compared the Microsoft.CSharp.Targets file with the mono.Csharp.target and make necessary changes.
But since there is in “ToolExe” property in framework v2.0
I can’t call gmcs.bat file.
In Toolpath property it searches for “csc.exe”
Csc is a task which wraps cs.exe.
Do I need to create a new task for gmcs.
OR is there any other way to add gmcs compiler in Csc.
{ FYI.
if I provide MonoLibPath as Toolpath in Csc task and rename gmcs.exe as CSc.exe.
It works..
compiles and runs fine too.
It uses the mono dll{system/Win Forms/ drawing}
It is kinda hack, don’t think it is a good way I guess
}
Thanks
August 19, 2009 at 4:58 pm
Thanks, great post and great work my friend!
I’ll let you know how I get on with it.
September 18, 2009 at 10:40 am
Thanks! One of the best posts I’ve read. Clear and to the point.
September 22, 2009 at 9:36 am
Thanks )))