Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The process of translating a C++ source file to an executable file #217

Open
Qingquan-Li opened this issue Aug 14, 2022 · 0 comments
Open
Labels

Comments

@Qingquan-Li
Copy link
Owner

Reference:
Book: Starting Out with C++ from Control Structures to Objects, byTony Gaddis, ninth edition

Source Code, Object Code, and Executable Code

1. Source Code -> Preprocessor -> Modify Source Code

When a C++ program is written, it must be typed into the computer and saved to a file. A text editor, which is similar to a word processing program, is used for this task. The statements written by the programmer are called source code, and the file they are saved in is called the source file.

After the source code is saved to a file, the process of translating it to machine language can begin. During the first phase of this process, a program called the preprocessor reads the source code. The preprocessor searches for special lines that begin with the # symbol. These lines contain commands that cause the preprocessor to modify the source code in some way.

2. Compiler (uncovers syntax errors) -> Object Code (contains machine language instructions)

During the next phase, the compiler steps through the preprocessed source code, translating each source code instruction into the appropriate machine language instruction. This process will uncover any syntax errors that may be in the program. Syntax errors are illegal uses of key words, operators, punctuation, and other language elements. If the program is free of syntax errors, the compiler stores the translated machine language instructions, which are called object code, in an object file.

3. Linker (combines the object file with the runtime library routines) -> executable code

Although an object file contains machine language instructions, it is not a complete program. Here is why: C++ is conveniently equipped with a library of prewritten code for performing common operations or sometimes-difficult tasks. For example, the library contains hardware-specific code for displaying messages on the screen and reading input from the keyboard. It also provides routines for mathematical functions, such as calculating the square root of a number. This collection of code, called the runtime library, is extensive. Programs almost always use some part of it. When the compiler generates an object file, however, it does not include machine code for any runtime library routines the programmer might have used. During the last phase of the translation process, another program called the linker combines the object file with the necessary library routines. Once the linker has finished with this step, an executable file is created. The executable file contains machine language instructions, or executable code, and is ready to run on the computer.

Translating a C++ source file imo an executable file

g++ -o hello hello.cpp

Translating a C++ source file to an executable file

The entire process of invoking the preprocessor, compiler, and linker can be initiated with a single action. For example, on a Linux system, the following command causes the C++ program named hello.cpp to be preprocessed, compiled, and linked. The executable code is stored in a file named hello.

Many development systems, particularly those on personal computers, have integrated development environments (IDEs). These environments consist of a text editor, compiler, debugger, and other utilities integrated into a package with a single set of menus. Preprocessing, compiling, linking, and even executing a program is done with a single click of a button, or by selecting a single item from a menu.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant