How to Convert a Simple .Net Console Project a into Portable Exe with Mono and Mkbundle

How to convert a simple .Net console project a into portable exe with Mono and mkbundle?

I have found a simple how-to here, however, as I have not tested it myself, I cannot guarantee results. As usual YMMV.

Quote from the original article (please follow the thread on the original article as well though):

Mkbundle: bundle Mono with your applications

Did you ever wonder why you need .NET Framework or Mono installed to run your program? Well, it would be much more handy if you could distribute your applications without nagging your clients to install additional frameworks, is it not? So here we are. Lets bundle a .NET-based application with Mono, so you don't need Mono, or .NET installed to run it.

Prepare an environment

First you need to install newest Mono and Cygwin. Installing Mono is very straightforward so you cannot screw up anything. When you start installing Cygwin, go into Full view, then please include 4 additional packages. These are: gcc, mingw, mingw-zlib and zlib.

Now you need a command prompt. Both Mono and Cygwin create shortcuts for command prompts on your desktop, but you need to combine them into one. Here is a batch that does it for me. You may need to change it, if you have other Mono version for example.

Code:

echo Mono version 2.4 Build 6
echo Prepending 'C:\PROGRA~1\Mono-2.4\bin' to PATH
PATH=C:\PROGRA~1\Mono-2.4\bin;%PATH%

chdir C:\cygwin\bin
bash --login -i

Bundle an application with Mono

So we are now in a command prompt, running this Cygwin mode. Notice that this is not a DOS prompt anymore, and "dir" won't work anymore. To list files use linux command "ls". The folder you are browsing now is like the one below. Arek is a username.
Code:

C:\cygwin\home\Arek

Browse to this folder with your explorer. Now you copy 2 files into this folder. 1st is your application exe and 2nd is the file Mono.dll (2MB) that you can find in your Mono folder.
Code:

C:\Program Files\Mono-2.4\bin

For some reason the whole procedure does not work with long file names, so rename your application exe. It should comply with this old DOS 8.3 naming.

Lets go back to command prompt. You need only 1 command to bundle your application, and here is some explanation.

mkbundle is a program within Mono package | -o Bundled1.exe specifies how the Mono-bundled exe will be named | Winform1.exe says what will be included, Mono libraries will be included anyway | --deps is necessary although I am not sure what it does | -z will compress the output exe a lot

Code:

mkbundle -o Bundled1.exe Winform1.exe --deps -z

So now you got your Bundled1.exe, which contains your own app along with Mono itself. You should not need Mono nor .NET to run it. Notice that it will be 4MB or more in size. Those bundled exes are not lightweight.

Mono exe asks for mscoree.dll

You can't expect a .Net program to run on windows (or any other platform for that matter) without a CLR like Mono or .Net.

You can however use the mono mkbundle program to produce a single binary statically linked with the mono runtime and with your assemblies packed inside. It is not really intended for windows use but I think it should work.

mkbundle also has license issues, as you end up including a static copy of mono ( which is GPL ) inside. So you should take note of this if you are considering third-party distribution.

How to use Xamarin or Mono to make a c# .net project available on multiple platforms?

mono/xamarin.ios/xamarin.android only provides a subset of the full .net runtime. Plus depending on what version of mac-os/ios etc that you choose you may have a smaller subset still.

You need to look at exactly what versions of each OS you want to support and then try and build your program/app around the commonly available features. If you've used features that aren't available you will need to rewrite them in a manner that works on all, or write platform specific code for each and use them via an interface.

Unless you're more specific on individual issues it's difficult to help you. It might be easier to look at the xamarin sample apps that do something similar to your app and try to follow their guidelines. They might have used better practices and it'll help you learn in the process.

EDIT

a comparison list of features that mono implements here. You'll have to dig into which version would work with you, I haven't worked with mono, only the xamarin variants.

How is Mono AOT / mkbundle used and optimized? (for reducing VM startup latencies)

If the AOT .so files exist beside the exe/dll files, Mono will use them when you run the exe. But you still need the dll/exe files for metadata and things that cannot be AOT-compiled.

Deploying C# (.NET 2.0) application as a portable application?

Well, other than things like Salamander and Thinstall (now VMWare ThinApp) you would have to have .NET installed if you really want to run .NET.

It may be possible to run Mono without actually installing it (not statically linking your program, but including Mono on the flash drive). I suspect it would be tricky though, as you'd have to tell the runtime about things like the GAC location.

I can't see anything in the Mono FAQ about this, but you might want to ping a Mono mailing list - it sounds like a potentially interesting and useful thing to be able to do.

c# app compiled using mono on windows for a zero install solution?

Please see my answer to a similar question here. I haven't performed the operation myself, but it may be of some help to you.

c# cywgwin mono mkbundle windows 7 - cannot compile file

Have you tried updating to Mono 2.6.1? I successfully got a Windows Form application working using mkbundle on Win7 x64 using the following steps:

  1. Download Mono 2.6.1
  2. Downloaded cgywin 1.7.1
  3. Installed packages gcc-mingw, mingw-zlib and pkg-config for cgywin
  4. Started cgywin and edited .bashrc e.g. C:/progra~2/notepad++/notepad++ $HOME/.bashrc
  5. Added $HOME/.bashrc export PATH=$PATH:/cygdrive/c/progra~2/Mono-2.6.1/bin
  6. Added $HOME/.bashrc export PKG_CONFIG_PATH=/cygdrive/c/progra~2/Mono-2.6.1/lib/pkgconfig
  7. Quit & restarted cygwin
  8. Changed directory to .Net application
  9. Compile the solution using xbuild
  10. Change directory to bin folder e.g. bin\Debug
  11. mkbundle -o Setup SetupForm.exe --deps -z
  12. Copied native mono dlls to bin\Debug folder (mono.dll, libglib-2.0-0.dll, libgthread-2.0-0.dll, zlib.dll)


Related Topics



Leave a reply



Submit