Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to code C# on Windows but run on Linux? [closed]

Tags:

.net

linux

mono

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?

like image 930
Luke Puplett Avatar asked Dec 04 '13 18:12

Luke Puplett


People also ask

What do I need to code in C?

To do that, you will need two different pieces of software: a compiler and a text editor. Both can be had for no cost.

Where can we code C?

Before anything else, you are going to need a text editor. Each operating system has its own editors already installed (Windows has Notepad, Linux distributions have Kwrite etc), but consider getting another. Notepad++ is an excellent editor you can use (and not only to write C code).

Is C hard to code in?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.

How does C code start?

Main() function: It is the starting point of all the C programs. The execution of C source code begins with this function.


1 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).
like image 116
ken Avatar answered Oct 02 '22 20:10

ken