了解编译器的工作方式 [英] Understand how the compiler works

查看:66
本文介绍了了解编译器的工作方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究C ++编程语言,并且对编译器如何创建可执行文件有疑问.我的书说:

I'm studying the C++ programming language and I have a questions about the how the compiler create an executable file. My book says:

C ++是一种编译语言,因此您需要将源代码转换为计算机可以执行的文件.该文件由编译器生成,称为目标代码(.obj),但是像hello world程序这样的程序由我们编写的部分和C ++库的一部分组成.

C++ is a compiled language so you need to translate the source code in a file that the computer can execute. This file is generated by the compiler and it's called the object code ( .obj ), but a program like the hello world program is composed by a part that we written and part of the C++ library.

hello world程序中的哪个部分?

Which is this part in the hello world program?

然后我的书说:

您必须编译并链接程序的两个部分以生成可执行文件.

You must compile and link the two parts of the program to produce an executable file.

这是否意味着可执行文件是链接程序两部分的结果,并且无法执行目标代码?

Does this mean that the executable file is the result of linking the two parts of the program and that the object code can't be executed?

推荐答案

这两个部分是:1)您的调用print函数并传递字符串的代码",这是您已编译的hello world程序;2)实现打印功能的代码(例如"std :: cout<<").我们称之为标准库.

The two parts are 1) "your code that calls the print function and passes the string in", which is your compiled hello world program; 2) the code that implements the print function (say "std::cout <<"). We call it standard library.

很明显,如果没有标准库对象文件,由于缺少std :: cout的实现,您将无法获得完整的可执行文件.

Obviously, without the standard library object file, you can't get a complete executable because of lack of std::cout's implementation.

存在 标准库的原因之一是使用户代码(您的hello world文件)在不同的操作系统之间更具可移植性,因为在Unix系统中,您实际上必须使用"write打印字符串的功能;但是在其他系统中,您可以使用写"以外的功能.标准库为您统一了它.

One of the reason that there is a standard library is to make the user code (your hello world file) more portable across different operating systems, because in Unix systems you actually have to use "write" function to print a string; but in other systems you may use functions other than "write". Standard library unifies it for you.

有关标准库对象文件的详细信息包括模板实例化之类的技术,您可能暂时对此不感兴趣.

Details about standard library object file include techniques like template instantiation, which you may be not interested in for now.

这篇关于了解编译器的工作方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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