Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Summary of Tencent written examination questions-- C program compilation process

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

1. Pre-compilation processing (cpp)

It mainly includes four processes.

1. Macro definition directives, such as # define N 6jinghundef, etc.

For the previous pseudo instruction, what the precompiler needs to do is to replace all N in the program with 6. Please note that this is a substitution, not copying 6 into N as a function argument. For the latter, a macro is undefined so that the N that appears later is no longer replaced.

two。 Conditional compilation instructions, such as # ifdef,#ifndef,#endif, etc.

The introduction of these directives allows programmers to decide which code to process by defining different macros. The precompiled program will filter out the unnecessary code based on the relevant files. In this way, you can reduce compilation time and improve efficiency during the compilation phase, and see what a good instruction this is. O (∩ _ ∩) O ~

3. The header file contains instructions, such as # include "file.h" or # include, etc.

In the header file, a large number of macros (the most common are character constants) are generally defined with the pseudo directive # define, which also contains declarations of various external symbols.

This approach allows us to directly call some complex library functions; on the other hand, it saves us the trouble of repeatedly doing some definition declaration work when writing the program. Just imagine, once we write the header file, then the relevant modules will no longer need to write these functions, directly # include OK, this is once and for all, ah, it is very cheap, hehe.

By the way, mention the difference between # include and # include "".

# include: this instruction tells the compiler to go to the default path of the system to find the relevant files.

# include ": this tells the compiler to look in the directory where the source program is located, and if not, go to the system default path.

4. Special symbols, the precompiler can recognize some special symbols.

For example, the LINE logo that appears in the source program will be interpreted as the current line number (decimal number), and FILE will be interpreted as the name of the C source program that is currently being compiled. The precompiled program is to replace these special symbols that appear in the source program with appropriate values.

The pre-compilation phase basically completes the replacement of the relevant code of the source program, so that the original intention of the program does not change, but the content of the code is different, so as to prepare for future compilation.

The following command is typically used for preprocessing:

Gcc-E hello.c-o hello.i

II. Compile and optimize the program (gcc/g++)

The compilation process is to generate the corresponding assembly code after a series of lexical analysis, syntax analysis, semantic analysis and optimization of the preprocessed files.

$gcc-S hello.i-o hello.s

Note: the current version of GCC combines the preprocessing and compilation steps into one step, which is completed with the cc1 tool. Gcc is actually some wrapper of the background program, calling other actual handlers according to different parameters, such as precompiled compiler cc1, assembler as, connector ld.

.section. Note.GNU-stack, "", @ progbits

Assembler (as)

At this stage, the assembly code is translated into the target file, which is already binary. In the windows environment, the suffix name of the file is .obj; in unix, there are o, .a, .ko and other files.

The target file consists of segments. There are usually at least two segments in a target file:

Code snippet: this paragraph contains mainly program instructions. The paragraph is generally readable and executable, but generally unwritable.

Data segment: mainly stores a variety of global variables or static data to be used in the program. General data segments are readable, writable, and executable.

You typically use the following command to assemble:

Gcc-c hello.c-o hello.o

IV. Link Program (ld)

Some people may wonder, the above object code is already machine code, that is to say, CPU can recognize these files, so why do we link programs? Yes! What to do with the included header files, and when our programs are distributed among many source files, this is what connectors do, and they need to be linked together after they are translated into object code before they can be executed. This is ok!

When it comes to the link of the function library, we also need to know the knowledge of the function library, which is divided into static link library (also known as static library * .lib) and link dynamic library (also known as dynamic library * .dll).

The link to the static library is compiled into the assembly file at compile time, which changes the file size, while the dynamic library is linked to the executable at execution time (double-click to run) when a file in the dynamic library is needed.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 265

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report