Friday, January 21, 2005

The Code Project - Merging .NET assemblies using ILMerge - .NET

The Code Project - Merging .NET assemblies using ILMerge - .NET

"As you know, traditional linking of object code is no longer necessary in .NET. A .NET program will usually consist of multiple parts. A typical .NET application consists of an executable assembly, a few assemblies in the program directory and a few assemblies in the global assembly cache. When the program is run, the runtime combines all these parts to a program. Linking at compile time is no longer necessary.

But sometimes it is nevertheless useful to combine all parts a program needs to execute into a single assembly. For example you might want to simplify the deployment of your application by combining the program, all required libraries and all resources into a single .exe file.

...

Since a .NET module is basically just an assembly without an assembly manifest, it should be possible to convert an assembly to a .NET module, at least that is what I thought. When researching this on google I found a tremendously useful tool on Microsoft research called ILMerge. This little gem makes it possible to link multiple assemblies to a single one.

First, you would compile your libraries to dlls and your program to an exe referencing the dlls. This is exactly what visual studio would do if you had multiple libraries and a program referencing these libraries, so there is no need to do this on the command line.

csc /target:library /out:ClassLibrary1.dll ClassLibrary1.cs
vbc /target:library /out:ClassLibrary2.dll ClassLibrary2.vb
vbc /target:winexe /out:Program.exe /reference:ClassLibrary1.dll, ClassLibrary2.dll Program.vb

This will produce a normal .exe that requires the two dlls in the program directory or in the global assembly cache to run.

Now you can link these parts to a single self-contained exe using ILMerge:

ILMerge /target:winexe /out:SelfContainedProgram.exe Program.exe ClassLibrary1.dll ClassLibrary2.dll

The nice thing about this is that you can also merge third party assemblies like commercial class libraries into your program. And you do not have to modify your build process. All you have to do is to merge the assemblies to a single exe before deploying.

..."


Very cool!

I've seen a third party product (Thininstall) that does something like this, as well as include the needed Framework components all in one EXE... But those kinds of products scary me a little. Not saying that ThinInstall doesn't sound VERY cool too though... I just wonder about testing, compatibility, support, etc, etc.

ILMerge is a MS utility, and one that is a little less scary. Its scope is less broad and just builds on the capabilities of the .Net Framework. i.e. No magic involved.

For more information, check out the ILMerge home page at http://research.microsoft.com/~mbarnett/ilmerge.aspx.

Here's a snip from the home page.

"ILMerge is a utility that can be used to merge multiple .NET assemblies into a single assembly. It is freely available for use from the MSR Downloads site. If you have any problems using it, please get in touch. (mbarnett _at_ microsoft _dot_ com)

ILMerge takes a set of input assemblies and merges them into one target assembly. The first assembly in the list of input assemblies is the primary assembly. When the primary assembly is an executable, then the target assembly is created as an executable with the same entry point as the primary assembly. Also, if the primary assembly has a strong name, and a .snk file is provided, then the target assembly is re-signed with the specified key so that it also has a strong name.

ILMerge is packaged as a console application. But all of its functionality is also available programmatically. While Visual Studio does not allow one to add an executable as a reference, the C# compiler does, so you can write a C# client that uses ILMerge as a library.

There are several options that control the behavior of ILMerge. See the documentation that comes with the tool for details.

The current version is 1.0.1816.20370 (created on 21 December 2004)."


Not only is it cool, but development on it seems pretty active.

Deploying .Net app's is already pretty easy (assuming the framework is already in place). This makes xcopy deployment even easier. I think this utility is a keeper...

2 comments:

Anonymous said...

Very very cool!
But if Class1.dll is a reference of Class2.dll, and register ClassUnion.dll (create with ILMerge, where parameters are Class1.dll and Class2.dll). I don't access Class1 of Class1.dll in ClassUnion.dll. Only access in Class2 in ClassUnion.dll. ClassUnion.dll register COM Interop in the system.
I write little english... Sorry.
Thanks!

Greg said...

In the end, I've only used ILMerge a couple times and never with COM interop... I wish I could have been of more help.