[Mono-list] Mono linux question

Kornél Pál kornelpal at hotmail.com
Wed Aug 3 04:00:15 EDT 2005


Just to know:

Altough /clr:pure generates 100% pure valid IL code, it will add CRT DLL
references using P/Invoke that will not run under Mono because Mono hasn't
got a compatible CRT DLL. Furthermore you even can't use MS CRT DLLs with
Mono because managed objects are passed using P/Invoke to CRT DLL and it is
not supported by the Mono runtime. In addition P/Invoke calls to
kernel32.dll are added as well to the assembly.

If you don't use CRT at all you can use it under Mono.

Using /clr:safe you cannot use CRT so it will run under Mono. Note that
/clr:safe uses a more strict syntax than /clr:pure so it may be better to
use /clr:pure.

I think that eliminating CRT references (replacing them with class library)
is simpler than rewriting the code in C#.

To compile CRT free assembly using /clr:pure do the following:

Use ignore all default libraries linker option to avoid CRT dependency. This
will generate a lot of linker errors for every CRT calls. You have to
replace CRT functions with class library functions.

You will notice that the liker wants use some overdecorated entry point
found in CRT. Just set the entry point to main.

There still will be a missing "?.cctor@@$$FYMXXZ". This is a static
counstructor for the assembly similar to DllMain but is supported for exe
files as well. This dependency cannot be avoided and is quite difficult
create a function with this signature.

Declare it as the following:

#pragma warning(disable:4483)

void __clrcall __identifier(".cctor")()
{
}

If you want you can add some initialization code to the constructor.

After these things you have to be able to run your /clr:pure assembly on
Mono. Note that the above steps are easy. The only difficult thing is that
you cannot use CRT functions.

Kornél

----- Original Message -----
From: "Jonathan Pryor" <jonpryor at vt.edu>
To: <JamesGHS at aol.com>
Cc: <mono-list at lists.ximian.com>
Sent: Wednesday, August 03, 2005 2:35 AM
Subject: Re: [Mono-list] Mono linux question


> On Tue, 2005-08-02 at 10:37 -0400, JamesGHS at aol.com wrote:
>> I am porting some C++ windows code to linux.  I came across code that
>> was using:
>>
>> #using <mscorlib.dll>
>> #using <System.xml.dll>
>>
>> using namespace System;
>> using namespace System::Xml;
>>
>> In my research i came across Mono.  I wanted to know if i loaded mono
>> onto linux if that code would still work in C++ on the linux box.
>
> It will not work.  The above code uses Microsoft's Managed Extensions
> for C++.  If you compile the code with VS.NET, you will get a "mixed
> mode" assembly, which contains both native machine code and managed IL.
>
> The native machine code won't work on x86 Linux (never mind on PPC or
> anywhere else) because it assumes the presence of Microsoft libraries
> (msvcrt.dll, calling conventions, hidden library features...).
>
> Attempting to recompile the code for Mono won't work, as there is no
> other Managed Extensions for C++ compiler.
>
> That leaves two choices:
>
> (1) Rewrite the code in C and C#, which can be handled portably between
>    Mono and .NET.  You might also try using SWIG (http://www.swig.org),
>    which will generate C wrapper code over existing C++ classes, and
>    may simplify the C++/C/C# integration work.
>
> (2) Port the Managed Extensions for C++ code to C++/CLI, the new,
>    improved, and ECMA-standardized replacement.  Then compile this code
>    with VS.NET 2005 using /clr:pure or /clr:safe (IIRC).  This should
>    generate 100% IL assemblies.
>
>    C++/CLI assemblies might not run on Mono *now*, but it will be
>    *possible* to run them eventually (as opposed to the current
>    mixed-mode assemblies, which will never run cross-platform).
>
> - Jon
>
>
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list



More information about the Mono-list mailing list