Combining Assemblies Using ILMerge
When creating solutions that include multiple projects, Visual Studio creates an assembly for each project. This can lead to libraries that are complicated to use, needing many references. Using ILMerge, the assemblies can be combined into a single file.
What is ILMerge?
ILMerge is a utility provided by Microsoft that allows a set of assemblies to be combined into a single file. This can be used to merge an executable file with its supporting dynamic linked libraries (DLLs) to allow you to distribute a runnable program as a single file. It can also be used to simplify large libraries that would otherwise include multiple DLLs with each needing to be referenced from the project that utilises them.
The ILMerge tool can be used as a command line utility or can be referenced within a project and used programmatically. In this article we will examine the basic functionality provided by the command line variation.
ILMerge is not distributed with the .NET framework and so must be obtained and installed separately. The installation file can be downloaded free-of-charge from the ILMerge download page of the Microsoft web site. Once downloaded, simply double-click the file to begin the installation process and follow the instructions provided.
The simplest syntax for the ILMerge command line utility requires that you provide two types of information. Firstly, the name of the new, merged assembly must be specified using the "/out" switch. Secondly, a list of assembly files to be merged is required. The first item in this list is considered to be the primary assembly. If the primary assembly is an executable file then the merged file will also be an executable. Similarly, the output file will be a DLL if the primary assembly is a class library.
For example, the following command could be issued to combine one primary assembly and two secondary assemblies into a single merged DLL file:
ilmerge /out:Merged.dll Primary.dll Secondary1.dll Secondary2.dll
NB: The path to the ilmerge.exe file has been excluded for clarity.
If you have a large number of assemblies to be merged, you can use wildcards within the list of secondary assemblies. To enable wildcards, you must also include the "/wildcards" switch.
ilmerge /wildcards /out:Merged.dll Primary.dll Secondary*.dll
When ILMerge runs successfully, there is little visible feedback. To review the operations that the tool performed, you can enabling logging to the screen with the "/log" switch:
ilmerge /log /out:Merged.dll Primary.dll Secondary1.dll Secondary2.dll
Often you will use ILMerge as part of an automated build procedure. In this case logging to the screen is not a viable option. Instead, you can use the "/log" switch and provide the name for a log file. The results of the merge operation will be stored in this log file in plain text.
ilmerge /log:log.txt /out:Merged.dll Primary.dll Secondary1.dll Secondary2.dll
11 July 2009