5,275,589 members and growing! (16,963 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » How To     Intermediate

Merging .NET assemblies using ILMerge

By Rüdiger Klaehn

Shows how to merge multiple .NET assemblies to a single one using ILMerge.
Windows, .NET, Visual Studio, Dev

Posted: 21 Jan 2005
Updated: 21 Jan 2005
Views: 64,974
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
30 votes for this Article.
Popularity: 6.12 Rating: 4.15 out of 5
4 votes, 13.3%
1
0 votes, 0.0%
2
1 vote, 3.3%
3
5 votes, 16.7%
4
20 votes, 66.7%
5

Introduction

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.

A single project

If all parts of your program are written by yourself in the same language, you can obviously just add all source files to a single project. The result will be a single DLL or EXE containing all dependencies.

csc /target:winexe /out:Program.exe 
      MainProgram.cs ClassLibrary1.cs ClassLibrary2.cs

However, if your program is written in multiple languages or if you are using binary third party libraries, you are out of luck.

.NET Modules

The .NET compilers already contain options for exactly this. If you compile a project, there is an option to create a module, which is similar to an assembly but without a manifest file. You can then use the al.exe tool to combine some of these modules to a single assembly. This feature makes it possible to create a single assembly that contains multiple languages.

First, you would compile the program and the class libraries to netmodules using the module target. Then you can use the assembly linker al.exe to combine these modules to a single assembly.

csc /target:module /out:ClassLibrary1.netmodule ClassLibrary1.cs
vbc /target:module /out:ClassLibrary2.netmodule ClassLibrary2.vb
vbc /target:module /out:Program.netmodule Program.vb
al /target:winexe /out:Program.exe ClassLibrary1.netmodule 
                 ClassLibrary2.netmodule Program.netmodule

But unfortunately, this method only works if you have all the required parts of your program either as source code or as .NET modules. If you are useing a third party class library in assembly form, you are again out of luck.

ILMerge

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.

Conclusion

I found ILMerge tremendously useful, and I think that something like this should be a part of the .NET framework SDK. Maybe just enhance al.exe so that it can also link DLLs.

I have only scratched the surface of the .NET build process and the capabilities of ILMerge, and this article might contain many inaccuracies or even errors. But I found ilmerge.exe so useful that I just had to write about it.

Resources

  • ILMerge: The ILMerge utility from Michael Barnett of Microsoft Research.
  • ILMerge Task: A NAnt task for ILMerge.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Rüdiger Klaehn


Rüdiger Klaehn works as freelance developer in the space industry. He is very interested in functional programming languages and hopes that .NET will lead to a more widespread adoption in the industry.
Occupation: Web Developer
Location: Germany Germany

Other popular .NET Framework articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 34 (Total in Forum: 34) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralWindows Mobile 6.0 CompatibilitymemberMandeep Singh Sidhu2:31 23 Apr '08  
GeneralCOMmemberGAbirkan9:33 22 Dec '07  
GeneralProblem after mergingmemberpadhalni1:56 19 Feb '07  
QuestionRe: Problem after mergingmembera85848:56 10 Apr '07  
AnswerRe: Problem after mergingmembera85849:04 10 Apr '07  
Questionmerging a bunch of DLLs of an ASP.NET projectmemberweareborg7:32 2 Dec '06  
GeneralLocalisation DLLsmemberDouba1:49 29 Jun '06  
GeneralGUI for ILMerge - GilmamemberTomer Shalev11:27 2 Dec '05  
GeneralThere is a product out there with UI for ILMergermemberSamar Aarkotti11:21 14 Oct '05  
GeneralRe: There is a product out there with UI for ILMergermemberFrank Hileman7:58 29 Nov '05  
Generalsimple question.memberSamar Aarkotti4:43 13 Oct '05  
General3rd party control licenses after mergememberDrew Noakes5:29 6 Sep '05  
GeneralRe: 3rd party control licenses after mergememberweiser_98@hotmail.com23:14 6 Feb '06  
GeneralRe: 3rd party control licenses after mergememberBertrand Jobert11:13 5 Nov '07  
GeneralDotfuscator and ILMergemembermceranski8:09 29 Aug '05  
GeneralRe: Dotfuscator and ILMerge [modified]memberqjhsert23:27 14 Oct '06  
Generalno outputmemberNewYoda23:21 12 Aug '05  
GeneralRe: no outputmemberDrew Noakes5:35 6 Sep '05  
GeneralHow to use thismemberallenmpcx21:46 12 Aug '05  
GeneralRe: How to use thismemberDrew Noakes5:38 6 Sep '05  
GeneralVB.NET and Setup ProjectsmemberAndrewChapman14:49 9 Aug '05  
GeneralRe: VB.NET and Setup Projectsmemberdcoolidge13:39 12 Aug '05  
GeneralRe: VB.NET and Setup ProjectsmemberAndrewChapman16:28 12 Aug '05  
GeneralRe: VB.NET and Setup Projectsmember__debug21:02 26 Aug '05  
GeneralILMerge Managed DirectX DLLsmemberDr_Lomax2:54 24 Jun '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 21 Jan 2005
Editor: Smitha Vijayan
Copyright 2005 by Rüdiger Klaehn
Everything else Copyright © CodeProject, 1999-2008
Web10 | Advertise on the Code Project