How to code C# on Windows but run on Linux?

Viewed 14421

I'm trying to get started with Mono but I'm struggling with the documentation, blogs and other questions on SO. Hopefully this question will be useful for others.

I'd like to write a data ingestion process in C# using VS2013, since that's my native tongue, but I need to run it on a Linux box. There is no UI, its a console application.

Assuming Mono (but not MonoDevelop) is installed on the target Linux box, do I need to compile my .csproj differently for running under the Mono 'CLR' on Linux, and if so, how do I compile a project, as opposed to examples that compile a single .cs file?

Should I install MonoDevelop for Windows, open my solution/project in that IDE and compile it for Linux, copy the output files and run the .exe there?

Otherwise will the IL in an MSBuild-compiled .EXE assembly 'just work' on Linux under Mono, and will referenced assemblies be loaded okay (assuming they're all in the output folder)? Must I copy over any .NET Framework assemblies?

2 Answers

A very good answer to your question lies in the Mono FAQ:

Is Mono Binary Compatible with Windows?

Yes, Mono is binary compatible with Windows. Which means that you can run binaries produced by .NET compilers from Microsoft and other vendors.

When porting your applications, you should make sure that you test its functionality as differences in the underlying operating system and differences in the VM implementations (bugs, missing features) might affect your application.

Mono does not have every .NET 1.1 API implemented (see the Mono release notes for Mono 1.0) and when executing a binary from Windows that consumes an unimplemented API you might get an obscure message about tokens not being found.

In these cases it is useful to compile your application with Mono's C# compiler just to ensure that you are consuming APIs that are supported.

This is not a perfect solution, as some APIs in Mono throw NotImplementedExceptions in certain cases, so you still should test your application with Mono. If you care about application portability, check MoMA, the migration analyzer.

Are there any reasons to build on Mono instead of using Visual Studio and copying the binaries?

In general, you can continue to use Visual Studio to write your code if you feel comfortable doing so.

Using Linux to develop will encourage you to test your software on Linux more frequently and if you have the chance, it will also help you to "dogfood" your own product.

This FAQ is a bit old (it's discussing Mono 1.0), but the above still holds true in my experience. It's always good to compile using Mono (or to develop in MonoDevelop or Xamarin Studio) to make sure you're not using any Microsoft-specific libraries.

At the very least, you should use a test suite that can run without Visual Studio (so, avoid the built-in Microsoft test tools). That way, you can run your tests on all the systems to which you plan to deploy. MonoDevelop ships with NUnit, which runs on Windows, Linux, and OS X.

Some quick, additional thoughts:

  1. Do NOT use the Windows path separator ("\") as a literal in your code. Use System.IO.Path.PathSeparator.
  2. Always use System.Environment.NewLine instead of line feeds (\n) and carriage returns (\r).

Mono, by design, is supposed to be compiled once and executed anywhere. If you compile the same program on Windows and Linux, they should be binary equivalents. You shouldn't have to do anything extra.

Read their FAQ, specifically the second question: "Is Mono Binary Compatible with Windows?"

Yes, Mono is binary compatible with Windows. Which means that you can run binaries produced by .NET compilers from Microsoft and other vendors.

Related