如何编写C ++库? [英] How C++ libraries are written?

查看:98
本文介绍了如何编写C ++库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,Windows操作系统使用汇编语言与硬件进行交互。

当他们这样做时,他们可以使用c,c ++或任何其他语言进行其余工作。



我知道C ++头文件(在C ++标准库中)实际上是为实现调用windows api。



那么头文件在哪里?它们是由编译器安装的吗?或者它们带有操作系统?



头文件用什么关键字或代码与sutable api交互(例如windows上的std :: cout,调用一个dll文件和linux中的函数另一个)?



例如iostream.h在Linux上与windows不同吗?



他们如何找到合适的库?



我的最后一个问题是,库如何与汇编代码交互?(所以汇编代码与硬件交互)



TIA。

As i know Windows operating system used the assembly language to interact with the hardwares.
When they did this,they could use c,c++ or any other language to the rest work.

As i know C++ header files(In C++ standard library) actually call the windows api for the implementaion.

So where are header files located? Are them installed by compilers ? or they come with the operating systems?

What keyword or code do the header files use to interact with the sutable api(for example std::cout on windows,calls a function in a dll file and in linux an other)?

For example is iostream.h different on linux from windows?

And how that how they find suitable libraries?

And my last question is that,how libraries interact with assembly code?(so assembly code interacts with hardware)

TIA.

推荐答案

你已经走错了方向:图书馆是不在头文件中。如果你第一次学习C ++和链接,然后以更正确的方式重复这些问题,那就好了。



我们可以回答:iostream.h对于Linix和Windows都是一样的。这是真的。但是这个事实只会误导你,因为它会错过更重要的声明:Linix和Windows的iostream库是非常不同的。这是因为头文件仅用于促进与库的接口,并且库在实现时就会出现。



为了开始这个,我建议你做一些简单的事情:一分钟,忘记所有那些标准或特定于操作系统的库。首先,考虑给出它们。这样做:创建自己的一些库。无论他们做什么,都没有什么用处。并研究那里的部署和使用。将您的库从您传递给某个客户时的情况建模,10包含源代码; 2)没有源代码。研究对象,库和头文件的使用,更重要的是什么,什么不是,这些文件来到项目的位置以及它们扮演的角色;什么是强制性的,什么不是。



完成本研究后,您将真正了解您的大多数问题,因为图书馆是一个图书馆;它是标准的,因为它足够通用且足够好,所以已经标准化了。



在你弄清楚那些更重要的事情之前,最好推迟回答你的另一个问题。



-SA
You are already going in wrong direction: the libraries are not in header files. It would be good if you first learned C++ and linking, and then repeated these questions, in a more correct ways.

Let's say, I could answer: iostream.h for Linix and Windows are the same. This is true. But this truth would only mislead you, because it would miss the more important statement: iostream libraries for Linix and Windows are very different. This is because header files are only used to facilitate interfacing with the library, and the library comes when it is implemented.

To start getting into this, I would advise you to do some simple things: for a minute, forget about all those standard or OS-specific libraries. At first, consider that they are given. And the do this: create some libraries of your own. No matter what they do, could be nothing really useful. And the, study there deployment and use. Model the situation when you pass your library from you to some customer, 10 with source code; 2) without source code. Study the use of object, library and header files, what is more important and what is not, where those files come to the project and what roles do they play; what is mandatory and what is not.

After you do this study, you will really understand the most of your issues, because a library is a library; it is standard only because it is universal enough and good enough, so has been standardized.

Before you figure out those more important things, it would be the best to postpone answering of your other question.

—SA


所有在一起的东西不是标题文件,但链接器。这就是你似乎缺少的主要观点。



链接器不仅结合了多个编译单元并形成了正确的程序,还提供了应用程序之间的链接和操作系统。只要程序调用操作系统服务,它就会以调用函数的形式执行。链接器实际上是知道如何处理该调用的人。



现在,链接器如何知道操作系统的所有这些API调用?操作系统将链接器呈现为具有数千个函数的大型库。所有这些知识都以库文件的形式提供给链接器。这就是音乐播放的重点。例如,Windows操作系统所使用的库称为kernel32.lib,user32.lib,gdi32.lib等。



头文件,相反,它是告诉编译器的一种方式,库中存在哪些库函数以及它们的调用约定。由于操作系统将应用程序作为一个大型库展示给自己,因此当然需要一个(或多个)头文件。这些与操作系统一起出现。但是,由于头文件只是一个文本文件,如果您知道操作系统提供的API调用的确切形式,您实际上可以自己编写。



所有这一切,这里有一些你的问题的答案(现在,我想,你已经自己想出来了):



一些头文件带有操作系统(连同相应的库文件),其他一些与编译器一起提供(例如iostream.h),并且其他一些文件也随附在您的项目中。



使用操作系统API没有特殊关键字。它们被称为通常的功能。 (这不是一个奇妙的伎俩!)



许多图书馆不直接使用操作系统,而是依靠更多基础库来做到这一点。例如,C ++的流媒体系统主要依赖于C ++运行时库,而C ++运行时库又包含对操作系统的调用。因此,每个操作系统的C ++运行时库都需要不同。



我希望这一点,以及谢尔盖在解决方案1中给你的好建议,回答你的问题。
What holds all together is not the header files, but the linker. And that is the main point you seem to be missing.

The linker not only combines multiple compilation units and forms a proper program, it also provides the linkage between an application and the operating system. Whenever your program invokes an operating system service it does that in form of calling a function. And the linker actually is the one who knows, what to do with that call.

Now, how does the linker know all these API calls of the operating system? The operating system presents itself to the linker as being a big library with thousands of functions. And all that knowledge is provided to the linker in the form of library files. So that is the point where the music plays. For example, the libraries that the Windows operating systems is dressed in are called kernel32.lib, user32.lib, gdi32.lib, and so forth.

Header files, in contrast, are a way of telling the compiler, which library functions exist in a library and what their calling conventions are. As an operating systems presents itself to application programs as a big library, you need of course a (or multiple) header files. These come in deed together with the operating system. But as a header file is only a text file, you could in fact write it yourself, if you know the exact form of the API calls the operating system offers.

All that said, here are some answer to your questions (and by now, I guess, you have already figured them by yourself):

Some header files come with the operating system (together with the corresponding library files), some others come with the compiler (e.g. iostream.h), and again others come with every library you integrate into your project.

There are no special keywords for using an operating system API. They are called just like a usual function. (Isn't that a marvelous trick!)

Many libraries do not make use of the operating system directly, but rely on more basic libraries to do that. For example the streaming system of C++ mostly relies on the C++ Runtime Library, which in turn contains the calls into the operating system. And hence, the C++ Runtime library needs to be different for every operating system.

I hope this, together with the good advice that Sergey gave you in Solution 1, will answer your question.


这篇关于如何编写C ++库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆