Welcome to MSDN Blogs Sign in | Join | Help

Hack the Build: Use ILMerge and MSBuild to Combine Multiple Assemblies into One

Jomo Fisher--Over the last few years I've been coding mostly in C#--before that, my day-to-day work was done in C++. I don't miss much about C++, but one of the things that I do miss is the ability to link many .LIB files into a single DLL or EXE

I've known that it was possible to do this in .NET (with LINK.EXE or ILMERGE.EXE) but there is no built-ine support in VS2005 for doing this.

This weekend, I decide to see what it would take to plug ILMERGE into the regular C# build system. It's pretty simple to get basic support.

(1) Download and install ILMerge MSI: http://www.microsoft.com/downloads/details.aspx?FamilyID=22914587-b4ad-4eae-87cf-b14ae6a939b0&displaylang=en

(2) Save the this file to C:\Program Files\msbuild\Ilmerge.CSharp.targets

(3) In VS2005, right-click on the projectin solution explorer and select "Unload Project"

(4) Right-click again and pick 'Edit MyApp.csproj'

(5) Replace the <Import> line with <Import Project="$(MSBuildExtensionsPath)\Ilmerge.CSharp.targets" /> and save.

(6) Right-click again on the project in solution explorer and select "Load Project"

(7) Build.

Now, the project you modified will automatically have all referenced assemblies merged-in. Only the assemblies marked "Copy Local" will be merged so system assemblies and GAC assemblies won't be merged by default.

You only need to modify the projects you would like to have merging. In other words, you can have mixed merged and unmerged projects in the same solution.

This posting is provided "AS IS" with no warranties, and confers no rights.

 

Published Sunday, March 05, 2006 5:13 PM by Jomo Fisher
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Встроим все библиотеки внутрь EXE! [Джомо Фишер]

Готовое решение. Выполняем по пунктам 1,2,3, и... Проект в Visual Studio сам соб
Monday, March 06, 2006 2:34 AM by Олег Михайлик

# Code Generation, ILMerge and Custom Build Tasks

I finished creating a code generator for Dice, and I turned the thing into a custom build task for MSBuild...
Thursday, March 23, 2006 9:20 AM by Life, Universe and Everything according to Dirk

# re: Hack the Build: Use ILMerge and MSBuild to Combine Multiple Assemblies into One

hi, I can build my project's main assembly, but in vs.net when ilmerge, it reports an error: exited with code -2146232576.
and can't merge successfully.

But i can merge all assembly successfully in command line.

Thanks.
Monday, April 24, 2006 9:16 PM by margiex

# re: Hack the Build: Use ILMerge and MSBuild to Combine Multiple Assemblies into One

okay i'm starting to think that by stumbling onto your blog i've made my way into an alternative reality where magical things are possible.

this is just full on. it's one thing to describe the rough concepts behind how such a thing might be achieved... but to go ahead and produce a (supposedly) working implementation... that's truly magical.

okay, my brain hurts from the very thought of it.  and it's all reminiscent of a joel spolsky article entitled 'please sir, may i have a linker?' which you ought to read if you haven't, as you've just shown how to solve what he said was a major shortcoming of .net.

cheers

lb

Thursday, April 05, 2007 12:28 AM by lb

# re: Hack the Build: Use ILMerge and MSBuild to Combine Multiple Assemblies into One

Hi. After several days trying to change the csproj to do the ilmerge after build, i found this post. Thx.

So, during my tests, although i could run the command in the cmd window, i always got errors like, "exited with code 1" or 123 or 9099 (or 9009 i dont remember).

Using this patch to csproj, i got the "exited with code 3".

Could you help me?

thx in advance

Wednesday, June 13, 2007 7:45 AM by Deraldo Silva

# Scott Hanselman's Computer Zen - Mixing Languages in a Single Assembly in Visual Studio seamlessly with ILMerge and MSBuild

# Mixing Languages in a Single Assembly in Visual Studio seamlessly with ILMerge and MSBuild

I really like the new LINQ to XML direct language support in VB9 . However, while it's cool, I'm not

Monday, October 08, 2007 4:09 PM by ASPInsiders

# re: Hack the Build: Use ILMerge and MSBuild to Combine Multiple Assemblies into One

Great! But...

In a project i use a DI-framework (Autofac) and MSTest. When i build the test lib, which references both my .exe and autofac.dll, it fails because it finds autofac classes both in the merged .exe and the .dll... If i remove the Autofac reference in the Test project it builds but the intellisens, and recognition of Autofac classes, is gone.

Friday, August 22, 2008 6:09 PM by CarlH

# Superb!

This is truly outstanding! You have no idea how giddy this makes me, to be finally able to link my assemblies together into a single cohesive unit is just... grand. I absolutely hate messy program directories with tons of scary looking files scattered about, and this lets me have a nice clean .exe file containing everything a user needs, all nicely bundled up and encapsulated.

I do have one question if you have the time to help me; it's not a life threatening situation or anything, but when I add a post-build call to signtool to sign my assembly with a digital signature, this occurs BEFORE the merge process, which subsequently renders the signature invalid. Is there any way I could make the post-build occur AFTER the merge?

Thanks a lot for this, it's really delightful and so far works like a charm.

Friday, November 14, 2008 9:04 PM by Logan

# re: Hack the Build: Use ILMerge and MSBuild to Combine Multiple Assemblies into One

Well I managed to solve this satisfactorily by moving the code from the include file directly into the project file and being sure to place it before the post-build (signing) action. Seems to work great. Thanks again for the awesome post!

Thursday, November 20, 2008 12:22 PM by chaiguy1337

# re: Hack the Build: Use ILMerge and MSBuild to Combine Multiple Assemblies into One

The custom target works well but the output file is no more signed (using a pfx file choose in the project properties).

Is there anything I missed to make it working ?

Monday, November 24, 2008 10:23 AM by Steve B.

# re: Hack the Build: Use ILMerge and MSBuild to Combine Multiple Assemblies into One

Thanks for this valuable information!

Is it possible to optimize the final assembly (exe) to remove code that is not used. Much like the C++ linker will discard stuff from LIBs that is not used, can ILMERGE do this for managed assemblies.

Use case: I have several general purpose "framework" libraries I reused in a bunch of places. In most cases, I only use 5-10% of the library code. When using ILMERGE to gen a single EXE, I'd like to optimize the final EXE size by stripping all the unused objects from the resulting EXE.

Friday, June 12, 2009 4:18 PM by Simon

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker