Click here to Skip to main content
Email Password   helpLost your password?

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

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralFailed to run
Tammam Koujan
5:51 19 Jan '10  
After merging successfully i get one EXE file but when i run it it is crush without any error message!?
GeneralILMerge GUI
Lee.
2:26 17 Oct '09  
I've Written a (rough) GUI for ILMerge, but according to MS Research, it(ILMerge) can't be redistributed. By the way, I <3 ILMerge.
GeneralMerged DLL com registration
William F
6:17 8 Jun '09  
Hello,
I want to merge my own .Net dll with a third party one in a new one, which has to be also COM compliant.
So, how can I generate the TLB file from the new merged dll?
Is all that possible and reliable ?

Thanks for answers.
GeneralHow do you merge all the assemblies in the bin folder using ILMerge using command line
azamsharp
6:07 24 Jul '08  
Hi,

The following code will merge a single assembly:
ilmerge /t:library /out:MyControlLibrary.dll App_something.dll

What if I want that all the assemblies in the current folder are merged together? How can I do that without explicitly typing the name of the assemblies?

Mohammad Azam
azamsharp@gmail.com
www.gridviewguy.com
Houston, TEXAS

GeneralWindows Mobile 6.0 Compatibility
Mandeep Singh Sidhu
2:31 23 Apr '08  
Hi,
I've a windows mobile 6.0 application, i've some managed as well as unmanaged Dll's and an exe file which serves as the entrance point of the application.
I tried to merge all these by using ILMerger(on command line) utility provided by Microsoft, and successfully got a desired output *.exe file.
Since i created the build on my local desktop i provided the lib path accordingly, the issue is that when i executed this file on my Windows Mobile 6.0 it gave me an error saying the assembly reference is missing(System.windows.forms....etc.)

so is it possible to use ilmerge for merging windows mobile 6.0 based libraries or executables???

kindly reply soon....

regards,
Mandeep
GeneralCOM
GAbirkan
9:33 22 Dec '07  
I am referencing a COM component in a VS C# 2003 project. This produces a .dll file, Interop.AX.dll (owing to COM) upon building the project into a .dll (FX.dll). I have to wrap/merge both dlls so that I can use it within a framework implemented in .net 1.1; VS .Net 2003. I ran ilmerge with these 2 dll files on a .Net 2.0 environment:

c:\>ilmerge /v1.1 /out: FX.dll Interop.AX.dll

I also tried c:\>ilmerge /v1.1 /out:FXnew.dll FX.dll Interop.AX.dll

but the latter didn't work giving an unresolved assembly reference erro. Although the first one produced a .dll I couldn't have it worked within the framework.

What do you think the problem can be?
Thanks
GeneralProblem after merging
padhalni
1:56 19 Feb '07  
Hi
I am using the following options in the PostBuildEvent in VS2005 (C# Project)

ILMerge.exe /wildcards /log:ILMerge.log /out:$(TargetDir)Output_Tool.exe $(TargetPath) $(TargetDir)Interop.*.dll

What I wanted to do is merge the exe and call the Interop dlls into the executable directory into

Output_Tool.exe.

If I build the project and see the log the unility seems to work fine, However when I copy the single exe into some other directory then it gives the "FileNotFoundException", that is because its looking for all the Interop dll's, which they should not be there as I dont want to distribute these dll's into the user system. However when i copy all these dll's into the new directory where i copied this "new" "merged" executable things seems to be working fine.

Any clues are greatly appreciated and I simply dont want to copy these Interop dll's into the "merged" assembly directory

Thanks
QuestionRe: Problem after merging
a8584
8:56 10 Apr '07  
I am havig a similar problem, my error is com class factory error, if I register the DLL it works, but I trying to make a self containing exe. Please help.
AnswerRe: Problem after merging
a8584
9:04 10 Apr '07  
The error I get is "Retrieving the COM class factory for component with CLSID failed du to the following error : 80040154
Questionmerging a bunch of DLLs of an ASP.NET project
weareborg
7:32 2 Dec '06  
I want to reduce the response time of an ASP.NET app and therefore I considered to merge several DLLs in the bin dir of the project to just a few DLLs. I have about 100 DLLs in the bin dir and want to reduce their number by merging to approx. 10.

Using the following command:

ILMerge.exe /target:library /out:Container.dll DLL-1.dll DLL-n.dll

I've got a huge DLL containing all merged DLLs.

My problem/question:

How can I tell IIS (in the web.config) where it should look for particular DLLs that were merged in the Container.dll?
GeneralLocalisation DLLs
Douba
1:49 29 Jun '06  
Hello, is there a way to merge localization dlls ?

Tried to add the path to the culture resource dll but it doesn't work on the worldcalc sdk sample...

I spent hours googling, no tips about statically linking satellite assemblies...

Any idea ?
GeneralGUI for ILMerge - Gilma
Tomer Shalev
11:27 2 Dec '05  
Me and Lior Brill have developed a graphical front end for this application
Gilma - GUI for ILMerge Application
It is released under the GNU license.

Enjoy!

GeneralThere is a product out there with UI for ILMerger
Samar Aarkotti
11:21 14 Oct '05  
Here is the link , i have not tried yet , but can't wait to try.

http://www.windowsmarketplace.com/prices.aspx?itemId=2124441&stext=
this is 129$ i guess, but free to try.
GeneralRe: There is a product out there with UI for ILMerger
Frank Hileman
7:58 29 Nov '05  
If it is just a wrapper, that is amazing! The link is dead, but ILMerge is trivial to use from the command line. A warning to all who offer free command line tools, some fool may put a UI on it and make money from your hard work.

check out VG.net: www.vgdotnet.com An animated vector graphics system integrated in VS.net
Generalsimple question.
Samar Aarkotti
4:43 13 Oct '05  
my project has bunch of DLL's and few bitmaps written in C# and one main .exe , how do i merge all of these into a single exe

can you please tell me in a simple way.

ILMerge /out (desired exe) :/ reference ( DLL1 , DLL2,DLL3,...etc)
and also /reference (Bitmaps,ICO's)

General3rd party control licenses after merge
Drew Noakes
5:29 6 Sep '05  
Is anyone else experiencing problems with compiled .licx license files after merging?

That is to say:

1) I compile my app. It has several assemblies. When run, the licensing works fine.
2) I use ILMerge to combine the assemblies. When run, I recieve licensing popup forms.

Drew.

Drew Noakes
drewnoakes.com
GeneralRe: 3rd party control licenses after merge
weiser_98@hotmail.com
23:14 6 Feb '06  
Yes I'm also having problems.

Anyone know of a fix?


GeneralRe: 3rd party control licenses after merge
Bertrand Jobert
11:13 5 Nov '07  
Same here, it seems that the .licx resource does not get properly converted to take into account the target assembly name so functions like GetSavedLicenseKey() fail to return a valid license. Has anybody found a fix for this?

Bertrand Jobert

GeneralDotfuscator and ILMerge
mceranski
8:09 29 Aug '05  
Has anyone had any luck with running ilmerge on dotfuscated assemblies? I have been trying to do this but I have had no success.
GeneralRe: Dotfuscator and ILMerge [modified]
qjhsert
23:27 14 Oct '06  
what error do u get? if it's the same one i got, then u need to use the option: /allowDup:DotfuscatorAttribute
Generalno output
NewYoda
23:21 12 Aug '05  
Hi,

ILMerge doesn't work with my assemblies.
I type:
ilmerge /target:winexe /out:newassembly.exe oldassembly.exe Interop.QuartzTypeLib.dll
there is no output file written and i have no output on the console
I use .NET 2.0 Beta 2

TIA
GeneralRe: no output
Drew Noakes
5:35 6 Sep '05  
You should add the following option:

/log:ILMerge.log

This will create a fairly detailed log of everything that happens, the good and the bad.

Note that the log file is appended to, so you may wish to create a script that deletes the previous log if you're running this often.

Good luck,

Drew.

Drew Noakes
drewnoakes.com
GeneralHow to use this
allenmpcx
21:46 12 Aug '05  
Hello, I am using C# and I have four .dlls I want to merge with my .exe, however when I use the post-build event, I get an error. This is what I type into the post-build event under Project Properties: ilmerge /out:$(TargetDir)Levitation.exe $(TargetPath) $(TargetDir)SoundFunctions.dll where Levitation.exe is the .exe file I want to merge with the SoundFunctions.dll. Am I doing this right? Thanks.


Mike - I love to program!
GeneralRe: How to use this
Drew Noakes
5:38 6 Sep '05  
Hey Mike,

This looks correct, although perhaps the ilmerge.exe isn't in scope. Do you get any messages in your 'Output' window?

As I mentioned in a different message, you should add /log:ILMerge.log to your command to catch the details of what's happening.

Drew.

Drew Noakes
drewnoakes.com
GeneralVB.NET and Setup Projects
AndrewChapman
14:49 9 Aug '05  
This seems to work fine in VB if the command line is used, but I was wondering what was the best way to incorporate the output into a setup project, particularly as the project is set up to use the non-merged source files rather than the merged output.


Last Updated 21 Jan 2005 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010