Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile a C# project into one dll

I have a base project that feeds two different projects. I want to be able to have the primary project compile all the references into one dll at compile time.

This needs to be something that is built into the project build so that Jr developers don't have to worry about it.


Solution: I ended up adding a line to the Post-build action of the project, so that a good build does the following:

$(ProjectDir)ilmerge.exe /out: /ver: <.dll> <.dll>

ilmerge.exe was moved to the project and the above line takes the dlls given and creates one.

like image 759
Omnia9 Avatar asked Aug 04 '11 13:08

Omnia9


2 Answers

(this answer was edited to update links to new download locations and to reflect new insights)

Your best bet is to use Micorosoft's ILMerge. It used to be available as a separate download from Microsoft Research (it is not part of VS or the .NET SDK).

The original version was maintained until 2012. It is now a NuGet package, which is the best way to get it presently, and to add it to your project and build server.

As an alternative, you may want to try ILMerge GUI from CodePlex, which provides a GUI interface to ILMerge. Its development was interrupted, but is now active again.

A Codeproject article on ILMerge explains more on how to use it and explains some use-case. An example command is:

ilmerge /target:winexe /out:AllInOne.exe NormalProgram.exe Lib1.dll Lib2.dll

As an alternative, you can use Fody with Costura.Fody, as explained in this SO response.


Note (1): if you need to merge side-by-side assemblies (i.e., mix native 64 bit and 32 bit assemblies), you should consult this SO article on loading side-by-side assemblies dynamically.

Note (2): to add this as an integral step to your build process, just go to Project Properties > Build Events > click "Edit Post-build" and fill in the command from above. Click "Macros" to see how to reference the current file or assembly.

like image 127
Abel Avatar answered Nov 06 '22 01:11

Abel


Have a look for the external tool ILMerge from Microsoft: http://research.microsoft.com/en-us/people/mbarnett/ilmerge.aspx It can merge multiple assemblies into just one.

like image 22
Sebastian P.R. Gingter Avatar answered Nov 05 '22 23:11

Sebastian P.R. Gingter