我正在学习c++,我刚刚开始学习Qt的一些功能来编写GUI程序。我问了自己以下问题:
c++以前没有语法能够要求操作系统提供一个窗口或一种通过网络进行通信的方式(我承认我也不完全理解api),它是如何通过c++本身编写的库突然获得这些功能的?在我看来,这一切都是可怕的循环。你能在这些库中找到哪些c++指令?
我知道这个问题对一个有经验的软件开发人员来说似乎微不足道,但我已经研究了几个小时,没有找到任何直接的回答。这已经到了我无法理解Qt教程的地步,因为库的存在对我来说是不可理解的。
我正在学习c++,我刚刚开始学习Qt的一些功能来编写GUI程序。我问了自己以下问题:
c++以前没有语法能够要求操作系统提供一个窗口或一种通过网络进行通信的方式(我承认我也不完全理解api),它是如何通过c++本身编写的库突然获得这些功能的?在我看来,这一切都是可怕的循环。你能在这些库中找到哪些c++指令?
我知道这个问题对一个有经验的软件开发人员来说似乎微不足道,但我已经研究了几个小时,没有找到任何直接的回答。这已经到了我无法理解Qt教程的地步,因为库的存在对我来说是不可理解的。
当前回答
为了提供与其他答案略有不同的观点,我将这样回答。
(免责声明:我只是稍微简化了事情,我给出的情况纯粹是假设的,是作为演示概念的一种手段而写的,而不是100%真实的生活)。
从另一个角度考虑问题,假设您刚刚编写了一个简单的操作系统,具有基本的线程、窗口和内存管理功能。你想要实现一个c++库,让用户用c++编程,做一些事情,比如创建窗口,在窗口上绘图等等。问题是,如何做到这一点。
首先,由于c++编译为机器代码,您需要定义一种使用机器代码与c++进行接口的方法。这就是函数的作用所在,函数接受参数并给出返回值,因此它们提供了在不同代码段之间传输数据的标准方式。他们通过建立一种称为调用约定的东西来做到这一点。
A calling convention states where and how arguments should be placed in memory so that a function can find them when it gets executed. When a function gets called, the calling function places the arguments in memory and then asks the CPU to jump over to the other function, where it does what it does before jumping back to where it was called from. This means that the code being called can be absolutely anything and it will not change how the function is called. In this case however, the code behind the function would be relevant to the operating system and would operate on the operating system's internal state.
所以,几个月后,你已经整理好了所有的操作系统功能。你的用户可以调用函数来创建窗口并在上面绘图,他们可以创建线程和所有美妙的东西。但问题是,你的操作系统的功能将不同于Linux或Windows的功能。所以你决定给用户一个标准的界面,这样他们就可以编写可移植的代码。这就是QT的用武之地。
As you almost certainly know, QT has loads of useful classes and functions for doing the sorts of things that operating systems do, but in a way that appears independent of the underlying operating system. The way this works is that QT provides classes and functions that are uniform in the way they appear to the user, but the code behind the functions is different for each operating system. For example QT's QApplication::closeAllWindows() would actually be calling each operating system's specialised window closing function depending on the version used. In Windows it would most likely call CloseWindow(hwnd) whereas on an os using the X Window System, it would potentially call XDestroyWindow(display,window).
很明显,操作系统有很多层,所有这些层都必须通过各种各样的接口进行交互。有许多方面我还没有谈到,但要全部解释它们将需要很长时间。如果您对操作系统的内部工作原理有进一步的兴趣,我建议您查看OS dev wiki。
请记住,许多操作系统选择向C/ c++公开接口的原因是它们可以编译为机器代码,它们允许将汇编指令与它们自己的代码混合在一起,并且它们为程序员提供了很大程度的自由。
同样,这里有很多内容。我想继续解释库如何像.so和.dll文件不需要用C/ c++编写,可以用汇编或其他语言编写,但我觉得如果我再添加更多,我还不如写一整篇文章,尽管我很想这样做,但我没有一个站点来托管它。
其他回答
When you try to draw something on the screen, your code calls some other piece of code which calls some other code (etc.) until finally there is a "system call", which is a special instruction that the CPU can execute. These instructions can be either written in assembly or can be written in C++ if the compiler supports their "intrinsics" (which are functions that the compiler handles "specially" by converting them into special code that the CPU can understand). Their job is to tell the operating system to do something.
When a system call happens, a function gets called that calls another function (etc.) until finally the display driver is told to draw something on the screen. At that point, the display driver looks at a particular region in physical memory which is actually not memory, but rather an address range that can be written to as if it were memory. Instead, however, writing to that address range causes the graphics hardware to intercept the memory write, and draw something on the screen. Writing to this region of memory is something that could be coded in C++, since on the software side it's just a regular memory access. It's just that the hardware handles it differently. So that's a really basic explanation of how it can work.
我认为你忽略了系统调用的概念。每个操作系统都提供了大量的资源和功能,您可以利用它们来做与操作系统相关的底层工作。即使当您调用常规的库函数时,它也可能在幕后进行系统调用。
系统调用是一种利用操作系统强大功能的低级方式,但使用起来可能复杂且麻烦,因此通常被“包装”在api中,这样您就不必直接处理它们。但实际上,几乎所有涉及O/S相关资源的操作都将使用系统调用,包括打印、网络和套接字等。
在windows的情况下,微软windows已经把它的GUI写进了内核,所以有一些系统调用来创建窗口,绘制图形等。在其他操作系统中,GUI可能不是内核的一部分,在这种情况下,据我所知,不会有任何与GUI相关的系统调用,您只能在更低的级别上使用任何可用的低级图形和输入相关调用。
计算机就像一个洋葱,它有很多很多层,从纯硬件的内核到最外层的应用层。每一层都将自己的一部分暴露给下一个外层,这样外层就可以使用内层的一些功能。
以Windows为例,操作系统为运行在Windows上的应用程序公开了所谓的WIN32 API。Qt库使用该API为使用Qt的应用程序提供自己的API。你使用Qt, Qt使用WIN32, WIN32使用较低级别的Windows操作系统,等等,直到它成为硬件中的电信号。
语言(如c++ 11)是纸上的规范,通常用英语编写。看看最新的c++ 11草案(或者从ISO供应商那里购买昂贵的最终规范)。
你通常使用带有语言实现的计算机(原则上你可以在没有任何计算机的情况下运行c++程序,例如使用一群人类奴隶来解释它;那样既不道德又低效)
你的c++实现通常工作在某个操作系统之上,并与它通信(使用一些特定于实现的代码,通常在一些系统库中)。通常这种通信是通过系统调用完成的。在instance into syscalls(2)中查找Linux内核上可用的系统调用列表。
从应用程序的角度来看,系统调用是一个基本的机器指令,就像x86-64上的SYSENTER一样,具有一些约定(ABI)。
在我的Linux桌面上,Qt库位于X11客户端库之上,通过X Windows协议与X11服务器Xorg通信。
在Linux上,在可执行文件上使用ldd来查看对库的依赖项的(长)列表。在正在运行的进程上使用pmap查看在运行时“加载”了哪些文件。顺便说一句,在Linux上,你的应用程序可能只使用免费软件,你可以研究它的源代码(从Qt,到Xlib, libc,…内核)以了解更多正在发生的事情
为了提供与其他答案略有不同的观点,我将这样回答。
(免责声明:我只是稍微简化了事情,我给出的情况纯粹是假设的,是作为演示概念的一种手段而写的,而不是100%真实的生活)。
从另一个角度考虑问题,假设您刚刚编写了一个简单的操作系统,具有基本的线程、窗口和内存管理功能。你想要实现一个c++库,让用户用c++编程,做一些事情,比如创建窗口,在窗口上绘图等等。问题是,如何做到这一点。
首先,由于c++编译为机器代码,您需要定义一种使用机器代码与c++进行接口的方法。这就是函数的作用所在,函数接受参数并给出返回值,因此它们提供了在不同代码段之间传输数据的标准方式。他们通过建立一种称为调用约定的东西来做到这一点。
A calling convention states where and how arguments should be placed in memory so that a function can find them when it gets executed. When a function gets called, the calling function places the arguments in memory and then asks the CPU to jump over to the other function, where it does what it does before jumping back to where it was called from. This means that the code being called can be absolutely anything and it will not change how the function is called. In this case however, the code behind the function would be relevant to the operating system and would operate on the operating system's internal state.
所以,几个月后,你已经整理好了所有的操作系统功能。你的用户可以调用函数来创建窗口并在上面绘图,他们可以创建线程和所有美妙的东西。但问题是,你的操作系统的功能将不同于Linux或Windows的功能。所以你决定给用户一个标准的界面,这样他们就可以编写可移植的代码。这就是QT的用武之地。
As you almost certainly know, QT has loads of useful classes and functions for doing the sorts of things that operating systems do, but in a way that appears independent of the underlying operating system. The way this works is that QT provides classes and functions that are uniform in the way they appear to the user, but the code behind the functions is different for each operating system. For example QT's QApplication::closeAllWindows() would actually be calling each operating system's specialised window closing function depending on the version used. In Windows it would most likely call CloseWindow(hwnd) whereas on an os using the X Window System, it would potentially call XDestroyWindow(display,window).
很明显,操作系统有很多层,所有这些层都必须通过各种各样的接口进行交互。有许多方面我还没有谈到,但要全部解释它们将需要很长时间。如果您对操作系统的内部工作原理有进一步的兴趣,我建议您查看OS dev wiki。
请记住,许多操作系统选择向C/ c++公开接口的原因是它们可以编译为机器代码,它们允许将汇编指令与它们自己的代码混合在一起,并且它们为程序员提供了很大程度的自由。
同样,这里有很多内容。我想继续解释库如何像.so和.dll文件不需要用C/ c++编写,可以用汇编或其他语言编写,但我觉得如果我再添加更多,我还不如写一整篇文章,尽管我很想这样做,但我没有一个站点来托管它。