Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static Linking of libraries created on C# .NET

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.

like image 342
claws Avatar asked Dec 08 '09 17:12

claws


People also ask

What is static linking in C?

Static linking is the result of the linker copying all library routines used in the program into the executable image. This may require more disk space and memory than dynamic linking, but is both faster and more portable, since it does not require the presence of the library on the system where it is run.

What is static library linking?

In computer science, a static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable.

How are static libraries used and created?

A static library is a file containing a collection of object files (*.o) that are linked into the program during the linking phase of compilation and are not relevant during runtime. As shown in the diagram above, when a program is compiled, the compiler generates an object file from a source file.

What happens when you link a static library?

Static Linking and Static Libraries is the result of the linker making copy of all used library functions to the executable file. Static Linking creates larger binary files, and need more space on disk and main memory.


2 Answers

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  
like image 108
Jon Skeet Avatar answered Sep 20 '22 09:09

Jon Skeet


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.

like image 34
Eric J. Avatar answered Sep 22 '22 09:09

Eric J.