61

I'm new to this, and don't know where to start.

I want to compile a Visual Studio C# project with Mono on Linux (by command line).

The main.cs file includes these references:

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using iTextSharp.text;
using iTextSharp.text.pdf;

I have no idea whether I need to note the references to the compiler, or whether it will pull them in itself, whether it will know where to look for them or not. I've never done this before. But I just need to compile this project.

Thank you!

Alasdair
  • 13,348
  • 18
  • 82
  • 138
  • 2
    There's one way to find out: paste the code into a Mono project, and try to compile it. The compiler will complain faster than we can tell you which references will work, and which won't. Mono has fairly comprehensive .NET library coverage, so it might work right out of the box. – Robert Harvey Nov 25 '11 at 02:34
  • Have you tried opening the Visual Studio solution in MonoDevelop? – dtb Nov 25 '11 at 02:34
  • Seriously, I have NO IDEA where to start. I don't know anything, I don't know what MonoDevelop is. Also I'm trying to compile it by command line on Linux. Will a Mono app compiled on Windows work on Linux? I assumed I had to compile it only on Linux for Linux. – Alasdair Nov 25 '11 at 02:39
  • OK, so I figured out MonoDevelop is the development part of Mono. I'm going to attempt to install this on Windows and try to compile it like suggested. Then see if the executable works on Linux. – Alasdair Nov 25 '11 at 02:50
  • It can't find iTextSharp libraries... I have the itextsharp.dll in the project directory. How can I point it at that? – Alasdair Nov 25 '11 at 02:51
  • monocs main.cs used to be how I did it on Linux – Icarus Nov 25 '11 at 02:52

5 Answers5

32

Have you tried xbuild? It's the MSBuild equivalent on the Mono world. If MSBuild is able to compile your project in Windows, xbuild should be able to do the same on Linux (if it doesn't, file a bug in Bugzilla).

UPDATE: Nowadays, Mono developers/packagers are in the process of bundling the original (and recently opensourced) MSBuild, which is now crossplatform. For example it's already available in the latest Mono installers for Mac, so you can use the command-line program msbuild to build your project/solutions (note: still a long way to go to be bundled by Linux distros because msbuild depends on nuget packages, aka binary blobs).

knocte
  • 16,941
  • 11
  • 79
  • 125
30

knocte is right, install mono-complete

apt-get install mono-complete

I don't know why but I had errors compling with only mono-xbuild installed,

and run from the command line :

xbuild yourvisualcsharpproject.csproj
Oli
  • 1,622
  • 18
  • 14
11

Mono and .NET Framework are not equivalent in terms of features available.

For example, Mono provides cross platform functions but doesn't implement some windows linked features provided by .NET Framework. So you can't compile with .NET on windows and expect it to run smoothly on Unix/Mono: It may work but there is a high chance it will fail. In fact, the more complex the program, the highest is the chance (you used something not available in Mono).

Compilation with command line tool: Xbuild

If you want to compile a .sln/.csproj from command line against mono, then xbuild (mono clone of MSBuild) is the way to go :

xbuild mysolution.sln
xbuild myproject.csproj

The man page of xbuild on Ubuntu.

xbuild.exe is located in mono installation, most likely in:

C:\Program Files (x86)\Mono\lib\mono\4.5

Compile from IDE (VS)

You can compile against Mono on Windows, from Visual studio with MonoHelper addin (using xbuild underneath).

There is also another solution, which is targeting a "Mono" .NET Framework profile from visual studio. Following steps come from here.

Note: I've tried to change the location of the profile in 4.5 (but that doesn't work; either vs doesn't find it or it detects it as not installed) so always use the v4.0 directory to install a profile based on v4.0 VM. This limitation and the fact that the "Profile" thing has been discontinued makes me thinks that it won't work for higher version of Tools.

The same profile trick is being used for Unity by the way.

  1. Create two registry keys:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319\SKUs\.NETFramework,Version=v4.0,Profile=Mono

    HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v4.0.30319\SKUs\.NETFramework,Version=v4.0,Profile=Mono

  2. Make a link to Mono directory inside of Microsoft References Assemblies Directory (you may need to run the following with administrator rights)

    cd "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile" mklink /d Mono "C:\Program Files (x86)\Mono\lib\mono\4.5" cd Mono mkdir RedistList cd RedistList notepad FrameworkList.xml

  3. Edit FrameworkList.xml

Paste the following inside FrameworkList.xml

<?xml version="1.0" encoding="utf-8"?>
<FileList  Redist="Mono-4.5" Name="Mono 4.5 Profile" RuntimeVersion="4.5" ToolsVersion="4.0" > </FileList>
  1. Change the framework target in the project application tab within Visual Studio

Compile with MonoDevelop/Xamarin Studio

MonoDevelop runs on Unix and on Windows, and you can easily switch from .Net compile/debug/run to Mono compile/debug/run within the IDE. However, while MonoDevelop is quite efficient for a C# seasonal developer, it has not reached yet the power of Visual Studio coupled with Resharper.

Fab
  • 14,327
  • 5
  • 49
  • 68
  • 1
    "This limitation and the fact that the "Profile" thing has been discontinued makes me thinks that it won't work for higher version of Tools." Can you please explain why it will not work for higher version of the tool? As I see it, you can make the link to any version of Mono (currently it is 4.5, but in the future it will be 4.6 or 4.7), and the referenced assemblies will point to those of Mono. – Cristian M Jan 13 '18 at 12:27
  • @CristianM I have no idea if it still works with visual studio 2017 for instance. Mono is a clone of .net framework which defines the specifications of toolset. As Mono/Xamarin and Microsoft now cooperates and .net core released, I didn't have time to check if the scenario still works for higher version and I don't think it will be supported by MS if you have any issue (with 4.6 or 4.7) – Fab Feb 27 '18 at 04:17
  • 1
    Ok. Thank you for your answer! – Cristian M Feb 27 '18 at 09:58
10

You can build in visual studio and deploy on Linux and run under mono.Have a look on this article

Mohamad Alhamoud
  • 4,881
  • 9
  • 33
  • 44
  • About the iTextSharp library... Visual Studio says it can't find it, even though itextsharp.dll is in the project directory. How can I point it at that, you can see above that it's included in the references. – Alasdair Nov 25 '11 at 03:06
  • I am not sure what is your problem , see this article about adding a dll to mono :http://www.mono-project.com/DllNotFoundException – Mohamad Alhamoud Nov 25 '11 at 03:36
  • @Alasdair have a using directive is not the same as having a reference to a dll. A using directive tells the **compile** to look for types in that namespace. A reference tells **MSBuild** to notify the compiler to use/include that particular dll in the compilation process – Rune FS Dec 06 '12 at 08:36
2

Visual Studio Code and Mono / .NetCore

If you are into running/compiling/debugging C# on Linux, you now can use

and if you want something with exactly the same api on linux and windows, you can use .Net Core

On the command line, to compile:

dotnet build <solution/project> 

or if you have only one project in the folder

dotnet build

and then

dotnet run
Fab
  • 14,327
  • 5
  • 49
  • 68