我正在学习c++,我刚刚开始学习Qt的一些功能来编写GUI程序。我问了自己以下问题:
c++以前没有语法能够要求操作系统提供一个窗口或一种通过网络进行通信的方式(我承认我也不完全理解api),它是如何通过c++本身编写的库突然获得这些功能的?在我看来,这一切都是可怕的循环。你能在这些库中找到哪些c++指令?
我知道这个问题对一个有经验的软件开发人员来说似乎微不足道,但我已经研究了几个小时,没有找到任何直接的回答。这已经到了我无法理解Qt教程的地步,因为库的存在对我来说是不可理解的。
我正在学习c++,我刚刚开始学习Qt的一些功能来编写GUI程序。我问了自己以下问题:
c++以前没有语法能够要求操作系统提供一个窗口或一种通过网络进行通信的方式(我承认我也不完全理解api),它是如何通过c++本身编写的库突然获得这些功能的?在我看来,这一切都是可怕的循环。你能在这些库中找到哪些c++指令?
我知道这个问题对一个有经验的软件开发人员来说似乎微不足道,但我已经研究了几个小时,没有找到任何直接的回答。这已经到了我无法理解Qt教程的地步,因为库的存在对我来说是不可理解的。
当前回答
好问题。每一个新的C或c++开发人员都有这样的想法。在本文的其余部分中,我假设使用的是标准的x86机器。如果你使用的是微软c++编译器,打开你的记事本并输入这个(命名为Test.c)
int main(int argc, char **argv)
{
return 0
}
现在编译这个文件(使用开发人员命令提示符)cl Test.c /FaTest.asm
现在打开Test。在你的记事本里。你看到的是翻译后的代码——C/ c++被翻译成汇编程序。你明白我的意思了吗?
_main PROC
push ebp
mov ebp, esp
xor eax, eax
pop ebp
ret 0
_main ENDP
C/ c++程序是设计来在金属上运行的。这意味着他们可以访问较低级别的硬件,从而更容易利用硬件的功能。比如说,我要在x86机器上编写一个C库getch()。
根据汇编器的不同,我可以这样输入:
_getch proc
xor AH, AH
int 16h
;AL contains the keycode (AX is already there - so just return)
ret
我用汇编程序运行它并生成一个。obj -命名为getch.obj。
然后我写了一个C程序(我不包括任何东西)
extern char getch();
void main(int, char **)
{
getch();
}
现在将这个文件命名为GetChTest.c。通过传递getch来编译这个文件。obj。(或者单独编译到.obj和LINK GetChTest。Obj和getch。Obj一起生成GetChTest.exe)。
运行GetChTest.exe,你会发现它在等待键盘输入。
C/C++ programming is not just about language. To be a good C/C++ programmer you need to have a good understanding on the type of machine that it runs. You will need to know how the memory management is handled, how the registers are structured, etc., You may not need all these information for regular programming - but they would help you immensely. Apart from the basic hardware knowledge, it certainly helps if you understand how the compiler works (ie., how it translates) - which could enable you to tweak your code as necessary. It is an interesting package!
这两种语言都支持__asm关键字,这意味着你也可以混合汇编语言代码。学习C和c++会让你成为一个更全面的程序员。
不需要总是与汇编程序链接。我提到它是因为我认为这会帮助你们更好地理解。大多数这样的库调用都利用了操作系统提供的系统调用/ api(操作系统负责硬件交互)。
其他回答
硬件是允许这一切发生的原因。您可以将图形存储器看作一个大数组(由屏幕上的每个像素组成)。要绘制到屏幕上,您可以使用c++或任何允许直接访问该内存的语言写入该内存。该内存恰好可以被显卡访问或位于显卡上。
在现代系统中,由于各种限制,直接访问图形存储器需要编写驱动程序,因此您可以使用间接方法。库创建一个窗口(实际上只是一个像其他图像一样的图像),然后将该图像写入图形内存,然后GPU将其显示在屏幕上。除了写入特定内存位置的能力之外,该语言不需要添加任何东西,这就是指针的作用。
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.
我如何看待这个问题,这实际上是一个编译器的问题。
这样看,你用汇编写了一段代码(你可以用任何语言写),它把你新写的语言(你想把z++称为汇编)翻译成汇编,为了简单起见,让我们称它为编译器(它是编译器)。
现在你给了这个编译器一些基本的函数,这样你就可以写int,字符串,数组等等,实际上你给了它足够的能力,所以你可以用z++来写编译器本身。现在你有了一个用z++编写的z++编译器,非常简洁。
更酷的是,现在您可以使用编译器已有的功能为其添加功能,从而通过使用以前的功能扩展z++语言的新功能
举个例子,如果你写了足够多的代码来绘制任何颜色的像素,那么你可以使用z++扩展它来绘制任何你想要的东西。
创建窗口不需要特殊的语法。所需要的只是操作系统提供一个API来创建窗口。这样的API由c++提供语法的简单函数调用组成。
此外,C和c++是所谓的系统编程语言,能够访问任意指针(可能由硬件映射到某些设备)。此外,调用在程序集中定义的函数也相当简单,这允许处理器提供的全部操作。因此,使用C或c++和少量的汇编程序来编写操作系统本身是可能的。
还应该提到Qt是一个不好的例子,因为它使用所谓的元编译器来扩展c++的语法。然而,这与它调用操作系统提供的api来实际绘制或创建窗口的能力无关。
好问题。每一个新的C或c++开发人员都有这样的想法。在本文的其余部分中,我假设使用的是标准的x86机器。如果你使用的是微软c++编译器,打开你的记事本并输入这个(命名为Test.c)
int main(int argc, char **argv)
{
return 0
}
现在编译这个文件(使用开发人员命令提示符)cl Test.c /FaTest.asm
现在打开Test。在你的记事本里。你看到的是翻译后的代码——C/ c++被翻译成汇编程序。你明白我的意思了吗?
_main PROC
push ebp
mov ebp, esp
xor eax, eax
pop ebp
ret 0
_main ENDP
C/ c++程序是设计来在金属上运行的。这意味着他们可以访问较低级别的硬件,从而更容易利用硬件的功能。比如说,我要在x86机器上编写一个C库getch()。
根据汇编器的不同,我可以这样输入:
_getch proc
xor AH, AH
int 16h
;AL contains the keycode (AX is already there - so just return)
ret
我用汇编程序运行它并生成一个。obj -命名为getch.obj。
然后我写了一个C程序(我不包括任何东西)
extern char getch();
void main(int, char **)
{
getch();
}
现在将这个文件命名为GetChTest.c。通过传递getch来编译这个文件。obj。(或者单独编译到.obj和LINK GetChTest。Obj和getch。Obj一起生成GetChTest.exe)。
运行GetChTest.exe,你会发现它在等待键盘输入。
C/C++ programming is not just about language. To be a good C/C++ programmer you need to have a good understanding on the type of machine that it runs. You will need to know how the memory management is handled, how the registers are structured, etc., You may not need all these information for regular programming - but they would help you immensely. Apart from the basic hardware knowledge, it certainly helps if you understand how the compiler works (ie., how it translates) - which could enable you to tweak your code as necessary. It is an interesting package!
这两种语言都支持__asm关键字,这意味着你也可以混合汇编语言代码。学习C和c++会让你成为一个更全面的程序员。
不需要总是与汇编程序链接。我提到它是因为我认为这会帮助你们更好地理解。大多数这样的库调用都利用了操作系统提供的系统调用/ api(操作系统负责硬件交互)。