Take the 2-minute tour ×

What are the best practices for writing code that can be cross compiled on .NET (windows) and Mono (linux)? Although I am very familiar with .NET, I am not that experienced in Mono and all its gotchas. Has anyone seen a good blog post or best practices paper on this, which I have not been able to dig up? I would be sticking with C# 3.0 level features.

Things that concern me is first of all Interop, since I would need to call some native code. Next would be the best ways to handle namespaces such as Mono.XXX. Should I be using a bunch of #if? Isolate the code in per-platform assemblies?

Any suggestions regarding architecture and design would be greatly appreciated! If you have had any experience in cross compiling for Linux/Mono in visual studio (any version), I would also be interested in that.

share|improve this question
cross compiling is the wrong phrase, you compile it once and then use it on both platforms. –  trampster Nov 26 '09 at 22:09
I don't think that's correct, given that Mono has a number of Mono.* namespaces, etc. Maybe in a perfect world? –  Will I Am Dec 16 '09 at 3:48
Well, I made an WinForms app without any P/Invoke (sound easy but it's hard to avoid P/Invoke) and it ran perfectly fine on Linux, the same .exe assembly VS2010 produced, and it even had use of freshly new .NET 4 features. –  Camilo Martin Dec 31 '11 at 18:29
add comment

5 Answers

up vote 7 down vote accepted

The biggest issues are sticking to the Mono-supported APIs. Using the Visual Studio Integration support in Mono can help a lot with this, since you can target Mono the entire time, on all platforms.

For your specific questions:

1) Interop - You'll need to stick to P/Invoke. Try to isolate this into separate, platform specific assemblies. This leads to 2:

2) Using #if - I would avoid this, and prefer to use an extensibility model. Mono supports the Managed Extensibility Framework, which provides a good way to "plug in" platform specific code at runtime.

share|improve this answer
Thanks for the pointer to the extensibility model. It shows it's a preview, not a shipping product. What are the chances the .net and mono implementations are going to stay in sync before the product is finalized? The visual studio integration plugin will definitely help, thank you. –  Will I Am Nov 24 '09 at 22:08
MEF is open source, so I wouldn't worry about it too much. It's going to be part of .NET 4, and it's pretty much stable at this point. We use it in our commercial product already. –  Reed Copsey Nov 24 '09 at 22:16
That is the best advice i've heard yet for MEF being used for cross platform to Mono. –  scope_creep Jan 18 '11 at 18:34
add comment

Check out The Mono Migration Analyzer

share|improve this answer
Thanks. Since this would be new code, I am not sure if I would make use of this tool (unless I'm missing something?). In other words I plan to use CI to built both the windows a linux builds. –  Will I Am Nov 24 '09 at 22:08
There are some parts of Mono that are not finished. For example few things in cryptography are missing. Those methods/classes are marked with 'todo' attributes. Moma will point out those not-implemented things to you. –  Miriam Nov 25 '09 at 5:55
add comment

You should be interrested by Prebuild :

Prebuild is a cross-platform XML-driven pre-build tool which allows developers to easily generate project files for major IDE's and .NET development tools including: Visual Studio .NET 2002, 2003, 2005, SharpDevelop, MonoDevelop, NAnt and Autotools.

share|improve this answer
Why the down vote ? The Prebuid project is very handy to maintain the same codebase using both .NET and Mono platform. And it's free. –  Larry Oct 12 '10 at 8:15
add comment

The Mono project provides a document with portability guidelines. That is a pretty good place to start.

share|improve this answer
If is worth noting that this document seems a little out-dated in places. That doesn't mean it is not full of useful information. –  Stewart Jul 22 '10 at 11:20
add comment

We use MonoDevelop and Visual Studio for development but what's key is to keep around a good NAnt build script to build the entire thing on a single shot (Joel Spolsky's rules).

The main point IMO is to state very clear that the software has to be cross-platform, so it is not about "porting to linux/mono" but actually developing each iteration on the required platforms.

We had to avoid some features at the beginning (using Mono/.NET for 5 years now for a commercial product) and we still stick to .NET Remoting, but that's not a big deal in multi-platform development in my opinion.

Counting on the soft debugger since almost one year is also a great thing.

share|improve this answer
add comment

Your Answer


