947
votes

What is the difference between a framework and a library?

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

22
@vba4all: How can this be a duplicate when it was asked first?Noob Saibot
Just because it was created first doesn't mean it is not a duplicate ;) it's just about identifying duplicate / highly similar questions, and ensuring resources / answers are shared (and the most active / best answered question open)GrayedFox
Possible duplicate of Framework vs. Toolkit vs. Libraryemlai
@tuple_cat actually your link seems to be the duplicate: it was asked after this onechharvey
We need some functionality, we call Library. We need some functionality we will take help of framework and Framework calls our code (Eg UIKit).Kamleshwar

22 Answers

383
votes

Actually these terms can mean a lot of different things depending the context they are used.

For example, on Mac OS 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++/Objective-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 the 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.

556
votes

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.

317
votes

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.

270
votes

You call Library.

Framework calls you.


図書館 助け
足場が痛い
多くの涙

261
votes

Library:

It is just a collection of routines (functional programming) or class definitions(object oriented programming). The reason behind is simply code reuse, i.e. get the code that has already been written by other developers. The classes or routines normally define specific operations in a domain specific area. For example, there are some libraries of mathematics which can let developer just call the function without redo the implementation of how an algorithm works.

Framework:

In framework, all the control flow is already there, and there are a bunch of predefined white spots that we should fill out with our code. A framework is normally more complex. It defines a skeleton where the application defines its own features to fill out the skeleton. In this way, your code will be called by the framework when appropriately. The benefit is that developers do not need to worry about if a design is good or not, but just about implementing domain specific functions.

Library,Framework and your Code image representation:

Library,Framework and your Code image relation

KeyDifference:

The key difference between a library and a framework is “Inversion of Control”. When you call a method from a library, you are in control. But with a framework, the control is inverted: the framework calls you. Source.

Relation:

Both of them defined API, which is used for programmers to use. To put those together, we can think of a library as a certain function of an application, a framework as the skeleton of the application, and an API is connector to put those together. A typical development process normally starts with a framework, and fill out functions defined in libraries through API.

114
votes

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.

48
votes

From Web developer perspective:

  1. Library can be easily replaceable by another library. But framework cannot.

    If you don't like jquery date picker library, you can replace with other date picker such as bootstrap date picker or pickadate.

    If you don't like AngularJS on which you built your product, you cannot just replace with any other frameworks. You have to rewrite your entire code base.

  2. Mostly library takes very less learning curve compared to Frameworks. Eg: underscore.js is a library, Ember.js is a framework.

44
votes

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.

31
votes

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 which calls your code.

22
votes

A framework can be made out of different libraries. Let's take an example.

Let's say you want to cook a fish curry. Then you need ingredients like oil, spices and other utilities. You also need fish which is your base to prepare your dish on (This is data of your application). all ingredients together called a framework. Now you gonna use them one by one or in combination to make your fish curry which is your final product. Compare that with a web framework which is made out of underscore.js, bootstrap.css, bootstrap.js, fontawesome, AngularJS etc. For an example, Twitter Bootstrap v.35.

Now, if you consider only one ingredient, like say oil. You can't use any oil you want because then it will ruin your fish (data). You can only use Olive Oil. Compare that with underscore.js. Now what brand of oil you want to use is up to you. Some dish was made with American Olive Oil (underscore.js) or Indian Olive Oil (lodash.js). This will only change taste of your application. Since they serve almost same purpose, their use depends on the developer's preference and they are easily replaceable.

enter image description here


Framework : A collection of libraries which provide unique properties and behavior to your application. (All ingredients)

Library : A well defined set of instructions which provide unique properties and behavior to your data. (Oil on Fish)

Plugin : A utility build for a library (ui-router -> AngularJS) or many libraries in combination (date-picker -> bootstrap.css + jQuery) without which your plugin might now work as expected.


P.S. AngularJS is a MVC framework but a JavaScript library. Because I believe Library extends default behavior of native technology (JavaScript in this case).

15
votes

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.

10
votes

here is linked a bitter article by Joel Spolsky, but contains a good distinction between toolboxes, libraries, frameworks and such

7
votes

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.

6
votes

I think library is a set of utilities to reach a goal (for example, sockets, cryptography, etc). Framework is library + RUNTIME EINVIRONNEMENT. For example, ASP.NET is a framework: it accepts HTTP requests, create page object, invoke lyfe cicle events, etc. Framework does all this, you write a bit of code which will be run at a specific time of the life cycle of current request!

Anyway, very interestering question!

6
votes

Libraries are for ease of use and efficiency.You can say for example that Zend library helps us accomplish different tasks with its well defined classes and functions.While a framework is something that usually forces a certain way of implementing a solution, like MVC(Model-view-controller)(reference). It is a well-defined system for the distribution of tasks like in MVC.Model contains database side,Views are for UI Interface, and controllers are for Business logic.

5
votes

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.

5
votes

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.

5
votes

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'

5
votes

I don´t remember the source of this answer (I guess I found it in a .ppt in the internet), but the answer is quite simple.

A Library and a Framework are a set of classes, modules and/or code (depending of the programing language) that can be used in your applications and helps you to solve an especific "problem".

That problem can be log or debuging info in an application, draw charts, create an specific file format (html, pdf, xls), connect to a data base, create a part of an application or a complete application or a code applied to a Design Pattern.

You can have a Framework or a Library to solve all these problems and many more, normaly the frameworks helps you to solve more complex or bigger problems, but that a consecuence of their main difference, not a main definition for both.

The main difference betwen a Library and a Framework is the dependency betwen their own code, in oder words to use a Framework you need to use almost all the classes, modules or code in the FW, but to use a Library you can use one or few classes, modules or code in the lib in your own application

This means that if a Framework has, for example has 50 classes in order to use the framework in an app you need to use, let said, 10-15 or more classes in your code, because that is how is designed a Framework, some classes (objects of that classes) are inputs/parameters for methods in other classes in the framework. See the .NET framework, Spring, or any MVC framework.

But for example a log library, you can just use a Log class in your code, and helps you to solve the "logging problem", that doesn´t mean that the log library doesn't have more classes in his code, like classes to handle files, handle screen outputs, or even data bases, but you never touch/use that classes in your code, and that is the reason of why is a library and not a framework.

And also there are more categories than Frameworks and Libraries, but that is off topic.

3
votes

I will try to explain like you're five.

Let's imagine that you had opened a burger restaurant in your city while ago. But you feel it's so hard to make a burger as a beginner. You were thinking about an easy way to make burgers for customers. Someone told you that there are McDonald Burger Framework and BurgerKing Burger Framework.

If you use McDonald Burger Framework, It's so easy to make Big Mac burger. (but you cannot make Whopper.)

If you use BurgerKing Burger Framework, It's so easy to make Whopper Burger. (however, you cannot make Big Mac)

Anyway, In the end, they are all burgers. An important thing here is, you have to follow their framework's rule to make burgers. otherwise, you feel even harder to make it or won't be able to make it.

And you also heard that there is something called Simple Burger-Patty Library.

If you use this Library, you can make whatever burger patty so easily (X2 speed). It doesn't really matter if you use McDonald Burger Framework or BurgerKing Burger Framework. Either way, you can still use this Simple Burger-Patty Library. (Even you can use this Library without frameworks.)

Do you see the difference between Framework vs Library now?


Once you started using McDonald Burger Framework. It would not be easy to switch to BurgerKing Burger Framework. Since you have to change the whole kitchen.

But Library, It would be much easier to switch others. or you can just not to use it.

1
votes

Library vs Framework

Martin Fowler - InversionOfControl

Library and Framework are external code towards yours code. It can be file(e.g. .jar), system code(part of OS) etc.

Library is a set of helpful code. Main focus is on your code. Library solves a narrow range of tasks. For example - utilities, sort, modularisation

your code ->(has) Library API

Framework or Inversion of Control(IoC) container[About] is something more. Framework solves a wide range of tasks(domain specific), you delegates this task to framework. IoC - your code depends on framework logic, events... As a result framework calls your code. It forces your code to stick to it's rules(implement/extend protocol/interface/contract), pass lambdas... For example - Tests, GUI, DI frameworks...

your code ->(has) and ->(implements) Framework API

[iOS Library vs Framework]

[DIP vs DI vs IoC]

1
votes

Based on the definitions given in the book Design Patterns by Erich Gamma et al.:

  • library: a set of related procedures and classes making up a reusable implementation;
  • framework: a set of cooperating classes with template methods making up a reusable specification. It sets the control flow and allows to hook into that flow for tailoring the framework to a specific problem by overriding in a subclass the hook methods called by the template methods in the framework classes.

Problem-specific code can use libraries and implement frameworks.