views:

266

answers:

4

Lately I've been working on a few little .NET applications that share some common code. The code has some interfaces introduced to abstract away I/O calls for unit testing.

I wanted the applications to be standalone EXEs with no external dependencies. This seems like the perfect use case for static libraries. Come to think of it third party control vendors could benefit from this model too.

Are there some hidden nasties with static libraries that I've missed?

Is there any reason why the C# designers left them out?

Edit: I'm aware of ILMerge but it doesn't offer the same convenience as static libraries.

+3  A: 

This is not directly related to C#, but to the whole .NET umbrella. Sure, it's possible to merge assemblies using ILMerge, but alas it's still a research project, but at least its license permits use for commercial endeavors. Other than that, the runtime (CLR) cannot be coalesced into a single executable, the target platform still needs .NET to be installed.

Edit:

I was typing this before you edited your post with ILMerge. I might be wrong but there is not additional benefits from avoiding the .NET-style dynamic linking at runtime. There's nothing wrong with putting the satellite assemblies together with the executable. Microsoft doesn't recommend putting them in the GAC.

Cecil Has a Name
I know it has no disadvantage but it's just easier to be able to give out one exe only.
L2Type
+1  A: 

Using static libs has the drawback that it is not patchable by the runtime provider (MS).

For example if you use static MFC libs and a securify issue or other bug is found in the MFC static libs, MS can not patch it (the code is already in your executable). Using shared Dlls allows for an easy patch at OS level without you caring about that.

Of course having shared libs has a small drawback: a dependency is taken and they must be installed on the system.

Dan Cristoloveanu
+1  A: 

See this article by Joel:

http://www.joelonsoftware.com/articles/PleaseLinker.html

John Buchanan
lol, back in 2004 he was expecting a new runtime every 6-12 months, huh.When in reality in the five years since that was written (January 2004), we've had exactly one new version of the runtime (version 2, in January 2006). Everything else has been libraries....In any case, it has a linker (obviously; _something_ has to resolve references between assemblies). It's just a dynamic, runtime linker.
DrPizza
+2  A: 

.NET does in fact support the moral equivalent of a static library. It's called a netmodule (file extension is usually .netmodule). Read more about it in this blog post. Beware that it isn't well supported by the Visual Studio build tool chain. I think extension methods are a problem as well. ILMerge is the better tool to get this done.

Hans Passant
That's very interesting thanks.
L2Type