库和 API 之间还有区别吗?
每当我询问人们 API 和库之间的区别时,我都会得到不同的意见。有些人给出了这种的定义,说API是一个规范和库是一种实现...
有些人会告诉您这种类型的定义, API 是一堆映射出来的函数,而库只是编译形式的分发。
所有这些让我想知道,在网络代码、框架和开源的世界中,真的还有实际的区别吗?像 jQuery 或 cURL 这样的库可以交叉到 API 的定义中吗?
另外,框架是否会跨入这一类别? Rails 或 Zend 是否有部分可以更像“API 式”或“库式”?
真的很期待一些有启发性的想法:)
Whenever I ask people about the difference between an API and a library, I get different opinions. Some give this kind of definition, saying that an API is a spec and a library is an implementation...
Some will tell you this type of definition, that an API is a bunch of mapped out functions, and a Library is just the distribution in compiled form.
All this makes me wonder, in a world of web code, frameworks and open-source, is there really a practical difference anymore? Could a library like jQuery or cURL crossover into the definition of an API?
Also, do frameworks cross over into this category at all? Is there part of Rails or Zend that could be more "API-like," or "libraryesque"?
Really looking forward to some enlightening thoughts :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我的观点是,当我谈到 API 时,它仅指暴露给程序员的部分。如果我说“图书馆”,那么我也指的是“在幕后”工作的一切,尽管它们仍然是图书馆的一部分。
My view is that when I speak of an API, it means only the parts that are exposed to the programmer. If I speak of a 'library' then I also mean everything that is working "under the hood", though part of the library nevertheless.
库包含可重用的代码块(软件程序)。
这些可重用的库代码通过API链接到您的程序
(应用程序编程接口)。也就是说,此 API 是一个库接口,通过它可将可重用代码链接到您的应用程序。
简单来说,API 是一种促进两个软件程序之间交互的软件接口。。
例如,在 C 等过程语言中,库
math.c
包含数学函数的实现,如sqrt
、exp
、log
等。它包含所有这些函数的定义。这些函数可以通过使用 API
math.h
来引用,其中 描述并规定预期行为。话虽这么说,API 是一种规范(
math.h
解释了它提供的所有函数、它们的参数和它们返回的数据等)和一个库是一个实现(math.c
包含这些函数的所有定义)。A library contains re-usable chunks of code (a software program).
These re-usable codes of library is linked to your program through APIs
(Application Programming Interfaces). That is, this API is an interface to library through which re-usable codes are linked to your application program.
In simple term it can be said that An API is a software interface that facilitates the interaction between two software programs..
For example, in procedural languages like C, the library
math.c
contains the implementations of mathematical function, such assqrt
,exp
,log
etc. It contains the definition of all these functions.These function can be referenced by using the API
math.h
which describes and prescribes the expected behavior.That being said, an API is a specification (
math.h
explains about all the functions it provides, their arguments and data they return etc.) and a library is an implementation (math.c
contains all the definitions of these functions).API 是库的一部分,定义了它如何与外部代码交互。每个库都有 API,API 是所有公共/导出内容的总和。如今API的含义已经扩大。我们也可以将网站/服务与代码交互的方式称为 API。您还可以知道某些设备具有 API - 您可以调用的命令集。
有时这些术语可以混合在一起。例如,您有一些服务器应用程序(例如 TFS)。它带有 API,并且该 API 是作为库实现的。但这个库只是你和执行你的调用的人之间的中间层。但如果库本身包含所有操作代码,那么我们就不能说这个库是 API。
API is part of library that defines how it will interact with external code. Every library has API, API is sum of all public/exported stuff. Nowadays meaning of API is widened. we might call the way web site/service interact with code as API also. You can also tell that some device has API - the set of commands you can call.
Sometimes this terms can be mixed together. For example you have some server app (like TFS for example). It has API with it, and this API is implemented as a library. But this library is just a middle layer between you and not the one who executes your calls. But if library itself contains all action code then we can't say that this library is API.
我认为
Library
是一组所有类和函数,可以从我们的代码中使用它们来轻松完成我们的任务。但是该库可以包含一些它不想公开的私有函数供其使用。API
是向用户公开的库的一部分。因此,无论我们有关于库的任何文档,我们都将其称为API 文档
,因为它只包含我们有权访问的那些类和函数。I think that
Library
is a set of all classes and functions that can be used from our code to do our task easily. But the library can contain some of its private functions for its usage which it does not want to expose.API
is a part of library which is exposed to the user. So whatever documentation we have regarding a library, we call it anAPI Documentation
because it contains only those classes and functions to which we have access.我们首先要定义一个接口...
接口:是两个“事物”相互交谈并交换信息的方式。 “事物”可以是 (1) 人类或 (2) 任何类型的运行代码(例如库、桌面应用程序、操作系统、Web 服务...等)。
如果人类想要与程序对话,他需要图形用户界面(GUI)或命令行界面(CLI)。两者都是人类(但不是程序)想要使用的界面类型。
然而,如果一个正在运行的代码(任何类型)想要与另一个正在运行的代码(任何类型)对话,它不需要或不需要 GUI 或 CLI,而是需要一个应用程序编程接口(API )。
所以回答最初的发帖者问题:库是一种运行代码,API 是该运行代码与其他运行代码对话的方式。
we have first to define an interface ...
Interface :is the means by which 2 "things" talk to each other and exchange information. "things" could be a (1) human or (2) a running code of any sort (e.g. library ,desktop application , OS , web service ... etc).
if a human want to talks to a program he need Graphical user interface (GUI) or command line interface (CLI). both are types of interfaces that humans (but not programs) would like to use.
if however a running code (of any sort) want to talk to another running code (of any sort) it doesn't need or want a GUI or CLI ,it rather need an Application Programming Interface (API).
so to answer the original poster question : library is a type of running code and the API is the means by which this running code talk to other running codes.
清晰简洁的语言
库:为可重用性而存储的所有类和方法的集合
API:用户可以在他/她的代码中使用的库类和方法的一部分。
In Clear and concise language
Library: Collection of all classes and methods stored for re-usability
API: Part of library classes and methods which can be used by a user in his/her code.
根据我的观点,无论调用者可以访问什么函数,我们都可以在库文件中调用 api,库文件具有一些私有函数,我们无法访问它们。
According to my perspective, whatever the functions are accessible to invoker , we can called as api in library file, library file having some of the functions which is private , we cannot access them ..
时,有两种情况,
我认为,在第一种情况下,从 API 角度思考是令人困惑的。这是因为我们总是使用图书馆。只有图书馆。没有库的 API 是不存在的,但人们倾向于这样思考。
C++ 中的标准模板库 (STL) 怎么样?这是一个软件库。
您可以拥有不同的库和相同的 API,这意味着一组可用的类、对象、方法、函数、过程或您在某种编程语言中喜欢的任何术语。但可以说,我们对一些“标准”库有不同的实现。
可以打个比方:SQL 是一个标准,但可以有不同的实现。你使用的总是一些实现 SQL 的 SQL 引擎。您可以仅遵循标准功能集,也可以使用一些特定于该实现的扩展功能。
除了此类库的不同实现的效率差异之外,库中的“幕后”内容不是您所关心的。
当然我知道,这种思维方式并不是什么“普遍约束力的标准”。只是创建了很多新术语,这些术语并不总是清晰、精确、直观,这带来了一些混乱。当 Oracle 谈论集合时。它不是库,也不是 API,它是一个“集合框架”。
There are two cases when we speak or think of API
I think, that in the first case, thinking in terms of API is confusing. It's because we always use a library. There are only libraries. API without library doesn't exist, while there's a tendency to think in such terms.
How about The Standard Template Library (STL) in C++? It's a software library.
You can have different libraries with the same API, meaning set of available classes, objects, methods, functions, procedures or whatever terms you like in some programming language. But it can be said, that we have different implementation of some "standard" library.
Some analogy may be that: SQL is a standard but can have different implementations. What you use is always some SQL engine which implements SQL. You may follow only standard set of features or use some extended, specific to that implementation.
And what "under the hood" in library is not your concern, except in terms of differences in efficiency by different implementations of such library.
Of course I'm aware, that this way of thinking is not what is a "generally binding standard". Just a lot of new terms have been created, that are not always clear, precise, intuitive, that brings some confusion. When Oracle speaks about Collections. It's not library, it's not API, it's a "Collections Framework".
在不使用技术术语的情况下,我想分享我对 API 和库的理解。
我区分“图书馆”和“API”的方式是想象我去图书馆的情况。当我去那里时,我向“图书馆员”索取一本我需要的书,而不知道整个图书馆是如何管理的。
我像这样在他们之间建立了一个简单的关系。
Without using technical terms I would like to share my understanding regarding API and library.
The way I distinguish 'library' and 'API' is imagining a situation where I go to a book library. When I go there, I request a book which I need to a 'librarian' without knowing how a entire library is managed.
I make a simple relation between them like this.
它们是完全不相关的概念。
库是一组可重用的代码。
API 是程序或库的一部分,允许外部代理与其交互。
They are completely unrelated concepts.
A library is a set of resuable code.
An API is the part of a program or library that lets external agents interact with it.