views:

2657

answers:

15

What is the difference between a framework and a library?

I always thought of a library as a set of objects and functions that is focussed around solving a particular problem or around a specific area of application development (i.e. database access); a framework on the other hand is a collection of libraries centred around a particular methodology (i.e. MVC) and covers all areas of application development.

Links (extracted from some of the answers below):

+33  A: 

A library performs specific, well-defined operations.

A framework is a skeleton where the application defines the "meat" of the operation by filling out the skeleton. The skeleton still has code to link up the parts but the most important work is done by the application.

Examples of libraries: Network protocols, compression, image manipulation, string utilities, regular expression evaluation, math. Operations are self-contained.

Examples of frameworks: Web application system, Plug-in manager, GUI system. The framework defines the concept but the application defines the fundamental functionality that end-users care about.

Jason Cohen
+1  A: 

Your interpretation sounds pretty good to me... A library could be anything that's compiled and self-contained for re-use in other code, there's literally no restriction on its content.

A framework on the other hand is expected to have a range of facilities for use in some specific arena of application development, just like your example, MVC.

Adam Bellaire
+1  A: 

A library implements functionality for a narrowly-scoped purpose whereas a framework tends to be a collection of libraries providing support for a wider range of features. For example, the library System.Drawing.dll handles drawing functionality, but is only one part of the overall .NET framework.

Jeff Yates
+1  A: 

I think you pinned down quite well the difference: the framework provides a frame in which we do our work... Somehow, it is more "constraining" than a simple library.
The framework is also supposed to add consistency to a set of libraries.

PhiLho
+1  A: 

Library - Any set of classes or components that can be used as the client deems fit to accomplish a certain task.
Framework - mandates certain guidelines for you to "plug-in" into something bigger than you. You merely provide the pieces specific to your application/requirements in a published-required manner, so that 'the framwework can make your life easy'

Gishu
+12  A: 

As I've always described it:

A Library is a tool.

A Framework is a way of life.

A library you can use whatever tiny part helps you. A Framework you must commit your entire project to.

James Curran
+14  A: 

I think that the main difference is that frameworks follow the "Hollywood principle", i.e. "don't call us, we'll call you."

According to Martin Fowler:

A library is essentially a set of functions that you can call, these days usually organized into classes. Each call does some work and returns control to the client.

A framework embodies some abstract design, with more behavior built in. In order to use it you need to insert your behavior into various places in the framework either by subclassing or by plugging in your own classes. The framework's code then calls your code at these points.

Panos
+6  A: 

I forget where I saw this definition, but I think it's pretty nice.

A library is a module that you call from your code, and a framework is a module calls your code.

Moe
But libc includes qsort(), which calls your code. I don't think that makes libc a framework.
Mark Baker
A: 

i think library is a seto of utilities to reach a goal (eg. Soket, Cryptogrrafy etc). Framework is Library + RUNTIME EINVIRONNEMENT. eg. asp.net is a framework: it accepts for http requests, create page object, invoke lyfe cicle events etc. Framework does all this, you write a bit of code wich will be runned at a specific time of the life cycle of current request! Anyway, very interestering question!

stefano m
A: 

This is how I think of it (and have seen rationalized by others):

A library is something contained within your code. And a framework is a container for your application.

Kon
+3  A: 

I like Cohens answer, but a more technical definition is: Your code calls a library. A framework calls your code. For example a GUI framework calls your code through event-handlers. A web framework calls your code through some request-response model.

This is also called inversion of control - suddenly the framework decides when and how to execute you code rather than the other way around as with libraries. This means that a framework also have a much larger impact on how you have to structure your code.

JacquesB
+8  A: 

Actually these terms can mean a lot of different things depending the context they are used. E.g. on MacOS X Frameworks are just libraries, packed into a bundle. Within the bundle you will find an actual dynamic library (libWhatever.dylib). The difference between a bare library and the framework on Mac is that a framework can contain multiple different versions of the library, it can contain extra resources (images, localized strings, XML data files, UI objects, etc.) and unless the framework is released to public, it usually contains the necessary .h files you need to use the library; thus you have everything within a single package you need to use the library in your application (a C/C++/Obj-C library without .h files is pretty useless, unless you write them yourself according to some library documentation), instead of a bunch of files to move around (a Mac bundle is just a directory on UNIX level, but the UI treats it like a single file, pretty much like you have JAR files in Java and when you click it, you usually don't see what's inside, unless you explicitly select to show the content).

Wikipedia calls Framework a "buzzword". It defines a software framework as

A software framework is a re-usable design for a software system (or subsystem). A software framework may include support programs, code libraries, a scripting language, or other software to help develop and glue together the different components of a software project. Various parts of the framework may be exposed through an API..

So I'd say a library is just that, "a library". It is a collection of objects/functions/methods (depending on your language) and your application "links" against it and thus can use the objects/functions/methods. It is basically a file containing re-usable code that can usually be shared among multiple applications (you don't have to write the same code over and over again).

A Framework can be everything you use in application development. It can be a library, a collection of many libraries, a collection of scripts, or any piece of software you need to create your application. Framework is just a very vague term.

Here's an article about some guy regarding the topic "Library vs. Framework". I personally think this article is highly arguable. It's not wrong what he's saying there, however, he's just picking out one of the multiple definitions of framework and compares that to the classic definition of library. E.g. he says you need a framework for sub-classing. Really? I can have an object defined in a library, I can link against it, and sub-class it in my code. I don't see how I need a "framework" for that. In some way he rather explains how the term framework is used nowadays. It's just a hyped word, as I said before. Some companies release just a normal library (in any sense of a classical library) and call it a "framework" because it sounds more fancy.

Mecki
A: 

bitter, but contains a good distinction between toolboxes, libraries, frameworks and such:

http://discuss.joelonsoftware.com/default.asp?joel.3.219431.12

Lorenzo Boccaccia
A: 

You call Library.

Framework calls you.

Ian Boyd
In Soviet Russia, does Library call you and you call framework?
Thomas Owens
In Soviet Russia, Framework owned by $tate.
Ian Boyd
A: 

Good diagram explanation here http://freejavaclass.com/articles/faq/difference_between_library_vs_framework.jsp

Kartik