Makefile learning (1): C/C++ compilation process

1. Introduction to GCC

1.1 Introduction

GCC official documentation https://gcc.gnu.org/onlinedocs/

  • Official documents are the most authoritative, and all answers on the Internet come from official documents
  • Domestic forums are uneven, it takes time to find a good answer, and it is easy to be misled by wrong documents. Therefore, it is recommended to read the official documents to be more reliable, and it can improve English reading ability

GCC(GNU Compiler Collection) The GNU Compiler Collection, on which all other open source software depends to some extent, and even other languages, such as PythonC, are developed with the GNU Compiler.

1.2 Common components of GCC

GCC is composed of many components, the common components are as follows:

  • c++: It is a version of gcc, the default language is set to C++, and the marked C++ library is automatically included when linking, which is the same as g++. (The functions of c++ and g++ components are basically the same, and we use them more often g++)
  • configure: A script in the root directory of the GCC source code, which is used to set up 配置值and create the make program file necessary for the GCC compiler
  • gcc: Mainly used to compile Clanguage programs, the driver is equivalent to executing the compiler and linker to produce the required output
  • g++: A version of gcc, the default language is set to C++, and the standard is automatically included when linking C++库, which is c++the same as
  • libgcc: The library contains routines that are part of the compiled program because they can be linked into the actual executable program. They are special routines that are linked into the executable program to perform basic tasks, such as floating point operations. , the routines in these libraries are usually platform-dependent
  • libstdc++: runtime library ( 动态库), containing all C++ classes and functions defined as part of the standard language

1.3 Common software included in GCC

The common software introduced below GCC, the compilation and operation of each software and program are closely related

  • ar: This is a program that maintains library files by adding, removing, and extracting files from the archive. This tool is used to create and manage object library files used by linkers. This program is part of the binutils package.
  • as: GNU 汇编器, which can be compiled or work on a variety of different platforms.
  • gdb: For GNU 调试器, whether it is used vscode or used vs2015, when debugging a C++ program, the bottom layer is to call GNUthe debugger to perform debugging tasks.
  • gprof: The program will supervise the execution process of the compiled program, and report the running time of each function in the program, and optimize the program according to the configuration file provided. (In actual work, the contact will be relatively small)
  • ld: GNU linker, use this program to link object files into executable programs or library files (used a lot in actual work)
  • libtool: A basic library that supports scripts that simplify the usage of shared libraries used by the make program's description file.
  • make: A utility program that will 读makefile脚本determine which part of the program 需要编译和链接and issue the necessary commands. The script it reads (called makefileor Makefile) defines file relationships and dependencies, 执行make进行自动批量编译程序freeing our hands.

1.4 GCC default header file search path

  • Header files are very important for compilation. When compiling a program, 一定要把需要依赖的头文件的搜索路径都要Includecome in.
  • When downloading some projects in github, I often encounter some very headaches. It will often report an error display 找不到xx头文件. After learning makefileto write, I believe that such problems can be easily solved.
  • GCCYou can view the default header file search path with the following command
echo | gcc -v -x c -E -
 /usr/lib/gcc/x86_64-linux-gnu/7/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/7/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include

insert image description here

2. Compilation process

2.1 Representation of hello world in computer

hello程序The life cycle of the program starts from a source program (or source file), that is, a text file created and saved by the programmer through the compiler, and the file name is hello.c.

A source program is actually a sequence of bits (also called bits) consisting of the values ​​0 and 1. The 8 bits are organized into groups called bytes, and each byte represents certain file characters in the program.

Generally, .cthe suffix is ​​the c language script file, and .cppthe suffix is c++​​the language script file. But in linuxthe system, the file suffix is ​​not distinguished. For it, a file is a string of bits without any other meaning. The 加后缀main reason is 使用者的习惯that it is easier for the computer to identify which language the file is written in. There is no difference.

Most computers use ASCIIthe standard to represent text characters

  • Represent each character with a unique one-byte integer-valued message
  • The hello.c program is stored in the file as a sequence of bytes

The representation method of hello.c illustrates a basic idea: all information in the system—including disk files, programs in memory, user data stored in memory, and data transmitted on the network 都是由一串比特表示的.

insert image description here

2.2 Compilation process

  • The life cycle of the program starts from a high-level C/C++ language program.

  • In order to run the program in the system, in the C language hello.c, each C statement must be converted 转化into a series 低级的机器语言指令(010101 format) by the underlying program.

  • Then these instructions follow a 可执行目标程序format called 打包好, and in 二进制磁盘文件the form of 存放, the object program is also called an executable object file.

  • The GCC compiler reads the program file hello.cand translates it into an executable object file hello, which 编译过程can be divided into 四个阶段completion, as shown in the figure below, the program ( 预处理器、编译器、汇编器和链接器) that executes these four stages together constitutes the compilation system (compilation system).

【picture】

2.2.1 Preprocessing stage

预处理器(cpp) #Modify the original Cprogram according to the command (header file) beginning with a character. For example, the command hello.con the first line #include <stdio.h>in tells the preprocessor to read stdio.hthe contents of the system header file and insert it directly into the program text, and the result is another Cprogram, usually .iwith .

2.2.2 Compilation phase

编译器(ccl) Translate the text file hello.iinto a text file hello.s, which is a 汇编语言program, and the assembler defines the function main as follows:

						main:
						subq   $8, %rsp
						mov1   $.LCO,%edi
						call   puts
						mov1   $0,%eax
						addq   $8,%rsp
						ret

Each statement describes a low-level machine language instruction in a text format. Assembly language is very useful, it provides a common output language for different compilers of different high-level languages

2.2.3 Compilation stage

汇编器(as): hello.sCompile into machine language instructions, package these instructions into a format called relocation object program, and save the result in the object file hello.o,

hello.oThe file is a binary file which contains 17 bytes which are mainthe instruction encoding of the function. If we open it in a text editor hello.o, we will see a bunch of gibberish

2.2.4 Linking phase

Note that the hello program calls the printf function, which is a function in the standard C library provided by every C compiler. printffunctions exist in a printf.oseparate precompiled object file called , and this file must somehow be incorporated into our hello.o program

连接器(ld)It is responsible for processing this kind of merger, and outputs the executable file hello file, which can be loaded into memory and executed by the system.

3. Storage of program in computer

The system takes a lot of time and steps to move information from one place to another:

  • The machine instructions for the hello program were originally placed on disk
  • When the program is loaded, they are copied to main memory
  • When the processor runs the program, the instructions are copied from main memory to the processor

Similarly, data strings hello world\nstart on disk, are copied to main memory, and are finally copied from main memory to the display device.

Guess you like

Origin blog.csdn.net/weixin_38346042/article/details/130725717