Linking all the way down 10

Posted by Jb Evain Tue, 22 Aug 2006 20:44:00 GMT

Do you remember this post?

If you do so, I’m sure you know, what I’m going to show you. A formidable use of the new linker.

What do you need? First you need an application. I’ve reused a very small program I wrote a bunch of month with db4o. It simply allows you to persist a few objects Person in a db4o database, and to retrieve them. But it’s sooo cool that I’d like to make it a standalone application.

It has a very friendly command line interface:

Very nice isn’t it? So now, let’s link it so that I have the minimum set of the assemblies my program use.

jbevain@avalon:/tmp/linker$ mono linker.exe -o persons -a persons.exe -p false
jbevain@avalon:/tmp/linker$ cp persons.yap persons/
jbevain@avalon:/tmp/linker$ cd persons/

The yap file is db4o’s database.

Please note that in SVN, I’ve disabled the ability to link the assembly mscorlib.dll. It’s a very special assembly which is of course very close to the runtime. So if ever you want to remove stuff from it, you have to be very frugal, so you don’t remove stuff used within the runtime. And it seems that I’ve not identified everything used. So it tend to be pretty unstable :)

But for the time being, it’s fine. Now, let’s create a standalone application from this great piece of code.

jbevain@avalon:/tmp/linker/persons$ mkbundle
OS is: Linux
Sources: 0 Auto-dependencies: False
Usage is: mkbundle [options] assembly1 [assembly2...]

Options:
    -c              Produce stub only, do not compile
    -o out          Specifies output filename
    -oo obj         Specifies output filename for helper object file
    -L path         Adds `path' to the search path for assemblies
    --nodeps        Turns off automatic dependency embedding (default)
    --deps          Turns on automatic dependency embedding
    --keeptemp      Keeps the temporary files
    --config F      Bundle system config file `F'
    --config-dir D  Set MONO_CFG_DIR to `D'
    --static        Statically link to mono libs
    -z          Compress the assemblies before embedding.

jbevain@avalon:/tmp/linker/persons$ mkbundle -o persons --nodeps ./persons.exe ./db4o.dll ./mscorlib.dll
OS is: Linux
Sources: 3 Auto-dependencies: False
   embedding: /tmp/linker/persons/persons.exe
   embedding: /tmp/linker/persons/db4o.dll
   embedding: /tmp/linker/persons/mscorlib.dll
Compiling:
as -o /tmp/tmp47dda943.tmp.o temp.s
cc -ggdb -o persons -Wall temp.c `pkg-config --cflags --libs mono`  /tmp/tmp47dda943.tmp.o
Done
jbevain@avalon:/tmp/linker/persons$ mkdir standalone
jbevain@avalon:/tmp/linker/persons$ mv persons standalone/
jbevain@avalon:/tmp/linker/persons$ mv persons.yap standalone/
jbevain@avalon:/tmp/linker/persons$ cd standalone/
jbevain@avalon:/tmp/linker/persons/standalone$ ./persons
Persons manager
l
All persons:
Person, name: Anaelle, age: 19
Person, name: Jb Evain, age: 23
Person, name: Reg, age: 24
q
jbevain@avalon:/tmp/linker/persons/standalone$

Et voilĂ , we now have a standalone application that works like a charm, and which contains, well almost, only what’s needed to use it. That’s so great!

Ok, let’s go back to the code, I need to make the linking against corlib working.

Trackbacks

Use the following link to trackback from your own site:
http://evain.net/blog/articles/trackback/208

Comments

Leave a response

  1. Avatar
    Marek Sieradzki Tue, 22 Aug 2006 22:17:55 GMT

    Just a small formal note: people not persons :)

  2. Avatar
    Jb Evain Tue, 22 Aug 2006 22:21:25 GMT

    Another frenchism :)

  3. Avatar
    Ergo Wed, 23 Aug 2006 00:20:50 GMT

    Mmm…

    Do you still need Mono to run it?

    Is the linked application now Linux-only?

    :)

  4. Avatar
    Jb Evain Wed, 23 Aug 2006 00:24:26 GMT

    Ergo: no, yes :)

  5. Avatar
    Andrew Tierney Thu, 24 Aug 2006 00:08:00 GMT

    Does the Linker/MkBundle work on Windows ?

    I understand linker/mkbundled exe’s will only work on their target platform.. Hence linking/mkbundling on linux produces only linux EXE…

  6. Avatar
    Julien Thu, 24 Aug 2006 02:19:04 GMT

    How ould this work with a project taht contains several assemblies. For example, if App.exe requires Apps1.ddl, Apps2.dll, etc. Can we run the linker on all these assemblies and get the minimum required for the union of all the assemblies?

  7. Avatar
    Jb Evain Thu, 24 Aug 2006 09:14:39 GMT

    Andrew: The linker works on windows, MkBundle does not. I don’t know MkBundle, source wise, so I can’t tell you if it’s portable.

    But the linker works on windows, and at least, you can use it like I described in my first post about the linker to reduce the dependencies size.

    If the linker works on MS’s .net framework? It does, but only for non core assemblies, as I can’t resign the core assemblies.

    Julien: Several assemblies are handled very well. The linker will produce a version of each assembly, containing all the code needed by the assembly you are linking and all its references.

  8. Avatar
    IG Thu, 24 Aug 2006 19:36:18 GMT

    since I cant link corlib in, can I deploy the linked app (without corlib) and corlib seperately ?

  9. Avatar
    Jb Evain Thu, 24 Aug 2006 20:29:43 GMT

    IG: As separate assemblies yes. But MkBundle produces one and only one binary.

  10. Avatar
    Timothy Fri, 25 Aug 2006 20:10:45 GMT

    I don’t understand… the mkbundle makes a standalone app, what does the linker do ?

Comments