933

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.

anbanm
  • 11,961
  • 5
  • 20
  • 17
  • 23
    @vba4all: How can this be a duplicate when it was asked first? – Noob Saibot Feb 03 '15 at 00:50
  • 5
    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 Nov 30 '15 at 15:20
  • 2
    Possible duplicate of [Framework vs. Toolkit vs. Library](http://stackoverflow.com/questions/3057526/framework-vs-toolkit-vs-library) – emlai Dec 02 '15 at 16:38
  • also related http://ux.stackexchange.com/questions/13314/pattern-library-vs-style-guide-what-are-the-differences – chharvey Jul 22 '16 at 19:52
  • 3
    @tuple_cat actually your link seems to be the duplicate: it was asked after this one – chharvey Jul 22 '16 at 19:57
  • 1
    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 Oct 14 '16 at 17:23
  • 1
    A software framework is not just a library. It also includes a particular design architecture which users must work within - a sort way in which different items in the library are connected together to provide a generic solution for a particular application type. Frameworks thus have the advantage of needing only configuration by the user and validation of pre-written tests to become operational. With libraries alone users need to design an architecture and create tests before implementation. So more work is needed with just the libraries. But libs are flexible & potentially more efficient. – Trunk Aug 14 '18 at 09:22

21 Answers21

550

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
  • 75,915
  • 26
  • 104
  • 111
  • 12
    +1 for real examples. Btw, do you mind clarifying what do you mean by a "web application system"? – Pacerier Aug 27 '14 at 03:35
  • 3
    @Pacerier probably something that lets you create a full web application, like Rails for Ruby or Sails for Node.js. – gustavohenke Dec 15 '14 at 13:49
  • Nice answer. Can you give some more examples related to iOS frameworks and libraries ? – NSPratik Jun 02 '15 at 06:25
  • or Django, the web framework for perfectionists with deadlines. – airstrike Mar 14 '16 at 02:02
  • 2
    I would think of filling out the skeleton with 'muscles' rather than 'meat'. I think it's more accurate analogy because 'muscles' are setting skeleton in motion. – altgov3en May 29 '17 at 08:31
  • @altgov3en 'meat' is required for setting a skeleton in motion; 'muscles' do not move a skeleton without other 'meat', eg nervous system meat, circulatory system meat – Geoffrey Hale Aug 01 '18 at 20:57
  • So now, Is Java Collection class a Framework or a Library? – Hadi Aug 02 '19 at 12:32
381

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.

Taryn
  • 224,125
  • 52
  • 341
  • 389
Mecki
  • 106,869
  • 31
  • 201
  • 225
  • 4
    Interesting that back in 2008 Wikipedia's article described "framework" as a "buzzword". – Zebrafish Jan 03 '18 at 14:11
  • 2
    IMO, a framework is the "blank" project environement. – Kulvar Jul 20 '18 at 15:56
  • I look at the framework as a skeleton or a frame with multiple empty slots and a framework defines what that slots should contain. So, the framework will use all those filled slots and create software. I feel it's kind of a plugin architecture where you plug certain things at certain places and forgot about making them work together. The framework takes care of that. Simple examples of such would be Processing(Creative Art), Spark(General purpose cluster computing), etc. – Nithin B Jun 14 '20 at 02:42
  • How is this answer still at the top, when it merely says the question is invalid. Some want to assign different definitions to these different words, and have, so lets talk about that. – Timothee Howland Feb 23 '21 at 16:59
  • @TimotheeHowland My answer is on top as it is the accepted answer. The questioner decides which answer is the accepted one, it is his question after all. And SO is not a discussion site. Also you are free to assign whatever meanings you like to words but then you are inventing your own language. Officially a library describes a collection of software routines and the origin of that term is well known, whereas a framework has no official definition and it is not even known where this term came from. – Mecki Feb 23 '21 at 22:12
313

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
  • 18,335
  • 6
  • 43
  • 54
  • 2
    @Panos Thanks for the explanation, but can you elaborate. For instance, I am using the FacebookSDK framework, and I am calling class methods from this framework. The FacebookSDK framework is not calling anything in my code, which is the opposite to your definition ie "don't call us, we'll call you." – Charles Robertson Dec 26 '16 at 21:24
  • 4
    @CharlesRobertson AFAIK FacebookSDK is a client library. It can not be classified as a framework and the reason is obvious (as you already noticed): The FacebookSDK is not calling anything in client code. Moreover Facebook [defines](https://developers.facebook.com/docs/javascript) the SDK as follows: "A rich set of client-side functionality for adding Social Plugins, Facebook Login and Graph API calls." No indication for framework... – Panos Dec 27 '16 at 10:19
  • 1
    @Panos Thanks. I guess the suitcase icon that represents the FacebookSDK is a little misleading then. And the name 'FacebookSDK.framework'. Facebook should rename it to something like 'FacebookSDK.dylib'? But thanks for clarifying this. It is good to know what the correct definition is... – Charles Robertson Dec 27 '16 at 18:47
  • That's an interesting definition. I have recently started using d3.js, and observed that it is generally considered as a framework. But whatever d3 code I wrote is within a usual javascript code, so I am unable to extend this definition to d3. – Dileep Kumar Patchigolla Jan 18 '19 at 13:04
  • 2
    @Dileep The [d3.js homepage](https://d3js.org/) states in the first sentence: "D3.js is a JavaScript library for manipulating documents based on data". I think that it is wrong to be considered as a framework. – Panos Feb 02 '19 at 16:28
265

You call Library.

Framework calls you.


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

Ian Boyd
  • 220,884
  • 228
  • 805
  • 1,125
  • 13
    Could you refactor this as a haiku? – Derek Tomes Mar 19 '14 at 22:28
  • 21
    @DerekTomes BTW: i added *haiku* form. – Ian Boyd Jun 01 '14 at 20:12
  • 58
    Your answer is being discussed in [META](https://meta.stackoverflow.com/questions/345434/popular-answer-saying-that-helping-the-library-hurts-many-tears-a-japanese-poem?noredirect=1#comment456695_345434). Would you care to translate the haiku? I think quite a few of us are wondering if it really does mean _Help the library, scaffolding hurts, many tears_ as Google translator quite fondly puts it :) – Bugs Mar 15 '17 at 13:49
  • 3
    Maybe, *The Library Helps, Framework Hurts, Many Many Tears*? – Lawrence Dol Mar 16 '17 at 02:57
  • 32
    Good to see that a mod undeleted the first two comments. Now it makes sense why the Japanese text was added (it was requested as a joke) and that it was just an attempt to translate, and not a real popular Japanese poem. – Zanon Mar 16 '17 at 10:33
  • @RobertMoore You sort of beat me to it but I was going to write framework calls you .... and add something of an insulting nature – Dexygen Jan 09 '19 at 01:53
  • 1
    @RobertMoore actually, in Russia the IoC principle is reversed and thus they call the framework xD – ranu Jun 18 '19 at 11:48
  • 1
    @ranu You mean soviet Russia, surely? – aross Nov 24 '20 at 16:37
257

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.

Community
  • 1
  • 1
Durai Amuthan.H
  • 28,889
  • 6
  • 148
  • 223
  • 16
    ***Inversion of Control*** Great in-depth explanation! – Patricia Aug 13 '14 at 22:21
  • 3
    Like the picture, sums it up nicelly, and shows also why sometime people get confused, because frameworks often bundle libraries too. – Didier A. Dec 10 '14 at 00:27
  • 1
    @didibus If you think answer gives good explanation ,you could upvote the answer so that post viewers can easily find the fruitful answer. – Durai Amuthan.H Dec 10 '14 at 06:17
  • So FFmpeg (not the program, but mainly LibAVUtil from the project) is a framework, and libavcodec, etc are libraries? – MarcusJ Dec 11 '14 at 00:16
  • 1
    @MarcusJ - Both LibAVUtil and libavcodec are libraries – Durai Amuthan.H Feb 25 '15 at 11:00
  • Your code is at a higher level than the library's low level modules. How does a Framework provide IoC with dependency injection if framework calls your code which in turn calls your library? – KannarKK May 10 '16 at 15:19
114

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
  • 95,648
  • 35
  • 171
  • 253
47

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.

Fizer Khan
  • 71,869
  • 26
  • 133
  • 149
44

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.

Adam Bellaire
  • 99,441
  • 19
  • 144
  • 160
JacquesB
  • 39,558
  • 12
  • 64
  • 79
31

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.

Aniket Kapse
  • 190
  • 1
  • 18
Moe
  • 24,940
  • 7
  • 50
  • 66
23

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).

Uday Hiwarale
  • 3,363
  • 4
  • 36
  • 44
15

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
  • 25,664
  • 11
  • 56
  • 84
10

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

Lorenzo Boccaccia
  • 5,707
  • 2
  • 17
  • 27
  • fix broken link https://web.archive.org/web/20180924092925/http://discuss.joelonsoftware.com/default.asp?joel.3.219431.12 – whyer Dec 08 '20 at 23:42
  • and give some quotes why you think the article is useful. it may get deleted again once, it's not good to give answers with bare links without quotes imo – whyer Dec 08 '20 at 23:43
7

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
  • 58,658
  • 18
  • 135
  • 183
  • 3
    Good point, that a _framework can contain libraries_. **So, it can be both, a framework in the first and a library in the second instance.** As this is the case for many web frameworks like [Apache Wicket](http://wicket.apache.org/meet/introduction.html), that define the processing cycle but also provide a big set of classes that implement concrete ui components. – OneWorld Oct 02 '12 at 11:01
  • I believe .NET is a very big library. A library too big to be called library, but still a library. Frameworks enforces a certain design, decides control flow etc with little room to tinker. .NET as such doesn't do that. I believe .NET is a library while ASP.NET MVC is a framework. But MS needs good words to market. – nawfal Nov 19 '14 at 20:37
6

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!

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
stefano m
  • 3,694
  • 4
  • 25
  • 24
6

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.

Community
  • 1
  • 1
Ravi shah
  • 61
  • 1
  • 1
5

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
  • 99,441
  • 19
  • 144
  • 160
5

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
  • 38,673
  • 6
  • 89
  • 128
5

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
  • 126,803
  • 45
  • 214
  • 298
5

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.

Diego
  • 31,278
  • 18
  • 81
  • 126
Junior Garza
  • 51
  • 1
  • 1
2

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.

Jin Lim
  • 766
  • 8
  • 15
1

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]

yoAlex5
  • 13,571
  • 5
  • 105
  • 98