Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking statically in C#

I'm working on a module for a CMS. This module is distributed as a class library DLL.

I have several utility libraries I'd like to use in this module. Is there anyway I can link these libraries statically so I won't have to distribute several DLL's (thereby distributing my utility libraries separately)?

I would like to have only one DLL.

like image 251
erlando Avatar asked Sep 02 '08 09:09

erlando


People also ask

What is dynamic linking in C?

Dynamic linking is a two-step process that relies on accessing the addresses of code. The first step occurs at compilation. When a file is compiled with a dynamic library, instead of copying the actual object code contained in the library, the linker simply scans the code contained and checks for missing symbols.

Should I link statically or dynamically?

The main difference between static and dynamic linking is that static linking copies all library modules used in the program into the final executable file at the final step of the compilation while, in dynamic linking, the linking occurs at run time when both executable files and libraries are placed in the memory.

How are libraries linked in C?

Shared libraries (also called dynamic libraries) are linked into the program in two stages. First, during compile time, the linker verifies that all the symbols (again, functions, variables and the like) required by the program, are either linked into the program, or in one of its shared libraries.


3 Answers

You can merge your many DLLs with ILMERGE:

http://research.microsoft.com/~mbarnett/ILMerge.aspx

Haven't tried it myself. Hope it helps.


Download here:
http://www.microsoft.com/downloads/details.aspx?familyid=22914587-B4AD-4EAE-87CF-B14AE6A939B0&displaylang=en

Brief Description (from download-page)
ILMerge is a utility for merging multiple .NET assemblies into a single .NET assembly. It works on executables and DLLs alike and comes with several options for controlling the processing and format of the output. See the accompanying documentation for details.

like image 71
Seb Nilsson Avatar answered Oct 14 '22 05:10

Seb Nilsson


If you don't want to use ILMerge, see this page:

http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition.aspx

editor's note: Jeffrey Richter advices to put your dlls into exe file as resources (For each DLL file you add, display its properties and change its “Build Action” to “Embedded Resource.”). Then a custom class loader is needed to make the executable work (At runtime, the CLR won’t be able to find the dependent DLL assemblies, which is a problem. To fix this, when your application initializes, register a callback method with the AppDomain’s ResolveAssembly event).

Be sure to change the resourceName string to point to your actual resources. (e.g. change AssemblyLoadingAndReflection to your project name.)

like image 35
Kaganar Avatar answered Oct 14 '22 05:10

Kaganar


The short answer for this is no! You can not link in a dll during compilation. I don't know if there is some subtle way to do this, but you would probably have to distribute the dlls along with your cms. The best way to do this is to make some kind of re-distributable.

like image 27
khebbie Avatar answered Oct 14 '22 05:10

khebbie