[Mono-dev] Cross Platform on Linux/Windows with Mono.Posix reference on Linux
Edward Ned Harvey (mono)
edward.harvey.mono at clevertrove.com
Tue Mar 29 12:49:04 UTC 2016
> From: mono-devel-list-bounces at lists.ximian.com [mailto:mono-devel-list-
> bounces at lists.ximian.com] On Behalf Of Greg Young
> <Reference Include="Mono.Posix" Condition=" '$(OS)' != 'Windows_NT' " />
(for clarification of my previous response, because I was asked for clarification off-list)
If you have some code that calls Mono.Posix.Something(), and you have a conditional reference to Mono.Posix in the project, then you're able to build on Mono, but on windows, without the reference, your call to Mono.Posix.Something() will not compile, which prevents you from building, even though at runtime you might never make that call. In order to compile on windows, you'll need to comment-out your Mono.Posix.Something() line, via #if or [Conditional], which means you need a compiler symbol to indicate whether you're building on mono or windows. This could be either separate project files, or multiple build configurations. (Or dynamically generated project files).
Maybe you can have a conditional reference to Mono.Posix on mono, and have a separate conditional reference to ../../ExtraClassLibraries/Mono.Posix.dll on windows, with copy-to-destination, so you can get the same code to build on both mono and windows, resulting in an effectively identical cross-platform binary... But I'm almost certain I recall somebody on this list telling me, that the JIT compiler can or will or might in the future, eagerly link assemblies at runtime (or just before runtime), so the Mono.Posix.dll file must be present on windows at runtime in order to avoid runtime exceptions, even though the runtime code execution will never execute that path.
You avoid all these problems by abstracting the functionality you want into an interface or abstract class, and using a factory to perform a runtime dynamic load of a mono-specific or windows-specific assembly with a derivative class. You can build the mono-specific assembly on mono, or maybe you can use the double-conditional described above and get the mono-specific assembly to also be buildable on windows. No need to copy Mono.Posix.dll to the destination, because Mono.Posix will only be called at runtime when run on mono.
More information about the Mono-devel-list