Call Mono C# Compiler - c#

For calling MS C# compiler there is CSharpCodeProvider (http://msdn.microsoft.com/en-us/library/microsoft.csharp.csharpcodeprovider.aspx)
but how do I call the Mono compiler?
I want to know if there was any errors after compilation.

Mono has a better approach available (which should be provided in .NET 5 probably),
http://tirania.org/blog/archive/2008/Sep-10.html

I have not used Mono, but a quick Bing turned up the following Microsoft.CSharp.CSharpCodeProvider in the Mono documentation, is it possible that Mono implements the functionality targeting the Mono C# compiler.

You should take a look at MonoDevelop. It's full featured IDE for the Mono project, speaking of C#, F# and others. You can create a project, generate the classes, paste your source and it'll take care of everything.
Speaking of the compiler:
It's called mcs if you exported mono to your PATH. man mcs/mcs --help.

Probably using Microsoft.CSharp.dll from Mono will call Mono's implementation of Microsoft C# compiler which is the same as Microsoft's.
To call Mono specific features of C# compiler use Mono.CSharp.dll

Related

What version of C# is used by Mono 2.6.4?

Perhaps (hopefully) I'm missing something obvious, but I can't see from the Mono site what version of C# is used for any particular version of Mono, and as a C# newbie I'm not sure how to find out directly myself.
My specific question is: what version of C# is used by Mono 2.6.4? (The reason I'm interested in that version is that it seems to be the base version of the Mono variant used by the current version of Unity 3D.)
The release notes have this to say:
If Mono 2.6.1 is configured with the preview mode, you will also get early access to Mono's C# 4.0 implementation. This implementation is now available as part of the dmcs command.
C# 4.0 named arguments and optional parameters have been fully implemented.
So, to be safe, keep to C# 3.0 features.
It seems to use 4.0 by the looks of this

Does C# require .NET

I'm new to C# (but not to programming) and I was wondering: do C# programs always require .NET, or are there ways to avoid dependencies and make the application independent?
Yes C# always requires the .NET runtime.
If you are worried about other platforms there is Mono which will allow .NET applications to run on platforms other than Windows (i.e. Linux) using the Mono runtime.
C# code is compiled into CIL code which is a platform-independent instruction set, I quote from Wikipedia:
During compilation of .NET programming
languages, the source code is
translated into CIL code rather than
platform or processor-specific object
code. CIL is a CPU- and
platform-independent instruction set
that can be executed in any
environment supporting the Common
Language Infrastructure, such as the
.NET runtime on Windows, or the
cross-platform Mono runtime.
A CLI runtime/interpreter is required, but it doesn't have to be .NET.
Currently there is one other CLI interpreter, MONO, for Linux.
C# isn't compiled to native code, and therefore the computer can't read it. You need the .Net framework to convert the so called bytecode (the code that the C# compiler compiles to, CLI) can be converted by the Just In Time compiler of the .Net framework.
Mono is an alternative framework, and it can also run C#. It is supported on more platforms then then the .Net framework (that only supports Windows).
So yes, either the .Net framework or the Mono runtime is needed to run C# applications, new versions of Windows automatically install and update the .Net framework.
Apparently .NET runtime is not required, see MonoTouch for example.
It is possible. C# is just a programming language, and just like any other programming: It can be compiled in any way you can think of: That includes compiling without .NET. In fact, the MONO project (C# for a lot of platforms) does just that.
There are some other commercial compilers available that will pretend to compile your application without .NET , but they just stick .NET in your executable: which is useless, slow and just stupid.
Any other way to use C# without .NET or MONO would be more of an educational experience than a practical solution. As for what the education experience is worth: If you have the time, I would definitely recommend trying something like it when you have a bit more experience.
Just as C programs typically require a C runtime, C# programs require the common language runtime.

How to use LuaInterface on Mono/Linux

When I try to use LuaInterface on Mono on Linux (using Mono 2.0 on Ubuntu 9.04) I get the following exception:
** (App.exe:8599): WARNING **: Method ':.DoDllLanguageSupportValidation ()' in assembly
'/home/ulrich/test/Debug/lua51.dll' contains native code that cannot
be executed by Mono on this platform.
The assembly was probably created using C++/CLI.
According to this web site LuaInterface can be used with Mono. MoMA says that too.
Is it possible to recompile lua51.dll to make it compatible to Mono?
LuaInterface looks to be pure C#, but it uses a mixed mode C++/CLI-ified version of the Windows version of the native Lua library, that mixes .NEt code and native 32-bit Windows code. There's no C++/CLI compiler for platforms other than Windows, so you can't port/recompile the C++/CLI code, though it should work on Mono on Win32 (or maybe Wine)..
The only really viable way to get this to work on Mono would be to make it use P/Invokes istead of C++/CLI. You could then use a dllmap so that when Mono tries to resolve the P/Invoke calls to lua51.dll, it is redirected to the Linux equivalent, liblua.so.5.1.
Older versions of LuaInterface use a pure P/Invoke wrapper. You could use this.
There are also a few attempts at alternatives, my own included. http://github.com/jsimmons/LuaSharp
For all of you reading this now: Use KopiLuaInterface!
See my post here: https://stackoverflow.com/a/21386450/1070906

Using Mono for developing in C++

I am starting to use Mono to develop applications in C# and C++. I wanted to ask you, how is Mono compiling the C++ code? is it using GCC? It is amazing to see that it has the STL containers... Also, can I use the Boost libraries and GSL libraries with Mono? Thanks in advance!!!
I think you must be using MonoDevelop, the IDE, as opposed to Mono itself.
Yes, MonoDevelop uses gcc/g++ to compile C/C++ source code, but it is not compiled to CIL - it is compiled to a native binary.
If I am understanding correctly, then you should be able to use boost just fine.
If, however, you are asking if Mono has support for Mixed-Mode assemblies or executables (e.g. assemblies/exe's that contain both native and .NET CIL), then I am sorry to inform you that this feature is not supported, nor is compiling C++ to pure CIL by Mono.
As long as you don't need mixed mode (i.e., forget the native part and go for CIL-only), mono does work with C++ code (I hear they're now experimentally supporting mixed mode, on Windows especially, and elsewhere via wine, but I think that part's NOT ready for prime time). The one well-supported C++ compiler at this time is Microsoft C++/CLI on Net 2.x frameworks; efforts have been underway (for many years now) to add gcc, but I don't know of any production-ready result so far:-(.

Cross platform programming on Windows

Two Questions:
Is there any way to write cross platform programs on Microsoft Visual Studio?
If there isn't then could I write a C# application on VS2008 and recompile it with MonoDevelop and have it work?
1 - I dont' think so. Not without something like Mono.
2 - Yes you can, but Mono doesn't cover all the framework - they are working on it.
The best thing to do is check with the Mono Migration Analyzer. The Mono Migration Analyzer (MoMA) tool helps you identify issues you may have on Mono - http://mono-project.com/MoMA.
I have found most of my .NET 2.0 applications can be converted, but you may need some tweaks.
You can always use C++ and QT. Soon QT will be released on LGPL license (from version 4.5) that will give some more freedom.
The only limit of using free QT license is that you don't get integration with VS. However this can be handled by using eg. CMake (which will generate VS solution files).
Yes, Write your code, compile and run on another platform using Mono. When you compile you generate IL, which Mono can use. Note: Some functions aren't available on Mono. Delphi Prism, is an add on for Visual Studio which allows you to code to Linux and Mac from VS albeit in the object Pascal language though.
Yes, see same issues as in 1
I agree with Joe90, just one thing he left out: MonoDevelop can compile .sln and .csproj files because it has a MSBuild implementation.
So point MoMA at your code and if you get a green light it should compile as-is in MonoDevelop.
As a MSCLR junkie I have to admit that Mono has a few 'better' implementations of certain critical functions (mostly to do with encryption). You will get more usability power from these.
Another thing to watch out for is subtle logic errors. If a class is implemented in Mono it does not mean that it will behave the same the MSCLR one (Mono is a cold-room implementation and as such they DO NOT use the original source code). This is really where you will get good results from a well unit-tested code base.
For a good indication of what you should expect, I remember seeing a large amount of #if MONO in the AgsXMPP repository.
You could ideally write a C# application and have it run on the Mono platform. BUT, that will depend on the libraries of MS .NET that has been ported yet to Mono.
Just in case, there is no language constraint, you could consider using other languages like Java, Python, Ruby and the like..
Good Luck!
As many others mention your success will depend on the libraries you use. Mono does have Winforms but I would suggest that you also look at GTK# http://www.mono-project.com/GtkSharp as your windowing library. If you use GTK# you will use a library which is not reverse engineered (as Winforms is in Mono).
My understanding is that "non-gui" .NET 2.0 stuff is pretty much in place with the newest versions of Mono
You don't need mono develop, the whole idea of mono is you can develop for .net and have the same assemblies work on both mono and .net, provided you only use stuff which has been implemented in mono.
Stuff which won't work:
pinvoke,
wpf,
linq to SQL
Stuff which will work:
.net 2.0,
c# 3.0 (including linq to objects and linq to xml),
winforms
If you write your application in Silverlight, anyone with a web browser and the Silverlight plugin can access your app. This is as cross-platform as you're going to get with .NET.

Categories