35

I'm using VS2008 C#.NET.

I created 3 different classes of libraries in 3 projects. I wrote an application which uses these libraries (dlls).

What is happening is each project is compiling into a class library. So, I've 3 dlls and 1 exe.

Instead I want to have these in two ways:

  1. Only class library assembly (dll) which contains 3 of them and 1 exe.
  2. just one EXE (everything inside it) :: static linking.

How could I do that? I cannot find any options for static linking in VS2008 also please mention commandline options too.

claws
  • 52,236
  • 58
  • 146
  • 195

5 Answers5

49

ILMerge is what you're after.

I'm not sure I'd really call this "static linking" - it's just merging several assemblies into one. (In particular, please don't get the impression that this is building a native, unmanaged executable.) However, I think it's what you're after :)

Update: ILMerge is now open source and is also available as a NuGet package:

Install-Package ilmerge 
timlyo
  • 2,086
  • 1
  • 23
  • 35
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I believe that in *nix land, an executable without dependencies on dynamically-loaded shared objects is called "statically linked" – orip Dec 08 '09 at 17:24
  • when its just one assembly whats problem in referring it as "static linking"? Because everything is linked into one and is along with my executable (I mean they are not shared) and everything is loaded at once too. – claws Dec 08 '09 at 17:31
  • 3
    @orip: My view of static linking is more than that though; linking in .NET is still somewhat dynamic - there's more to be fixed up at execution-time than with a statically-linked native executable. – Jon Skeet Dec 08 '09 at 17:32
  • 4
    @jonskeet I agree, I've been searching all over stackflow & all the .netters think that ilmerge is statically linking. The goal of static linking (including in Windows in the days of c/c++ and compilation) was always to have a stand-alone exe that needed absolutely nothing else... I better just repeat that: absolutely nothing else except the exe in question. Not a single other dll required or assumed. – PandaWood Nov 17 '10 at 03:12
  • Could you use ILMerge to link in the .NET Fx DLLs too and make the .exe standalone? – kdmurray Dec 01 '10 at 09:16
  • @kdmurray: No. ILMerge just creates a single .NET assembly - it still requires the CLR etc to run. – Jon Skeet Dec 01 '10 at 09:25
  • 1
    @kdmurray: With my answer, you can create a single .NET assembly that does not rely on the .NET runtime. – Eric J. Oct 01 '12 at 03:13
10

You can place all of your code into one EXE project, use a third-party linker (google .net static linker for a number of options), or use ILMerge as illustrated here.

The third-party linkers generally also offer code obfuscation, and some can also statically link the .NET Framework.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
7

Stumbled across this question, and found another method of achieving these ends from Jeffrey Richter (http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition.aspx): you can embed your .dll into your executable as a resource, then overload the AssemblyResolve event to load the assembly that way. Quoting the code from the link:

AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => {
   String resourceName = "AssemblyLoadingAndReflection." +
      new AssemblyName(args.Name).Name + ".dll";
   using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) {
      Byte[] assemblyData = new Byte[stream.Length];
      stream.Read(assemblyData, 0, assemblyData.Length);
      return Assembly.Load(assemblyData);
   }
};
Aidan Cully
  • 5,457
  • 24
  • 27
6

In regards to Jon Skeet's answer, ILRepack is also a good open-source alternative to ILMerge. You can install it from NuGet via:

Install-Package ILRepack
Community
  • 1
  • 1
James Ko
  • 32,215
  • 30
  • 128
  • 239
2

You should also have a look at ILMERGE-GUI. It's a GUI wrapper for ILMerge.

Paul Hutchinson
  • 1,578
  • 15
  • 21