introduction:
It is now 8:16 on June 14, 2023, Beijing time. The final exam is coming soon, and the focus may have to shift to the exam. But I am very happy to think that I can go home to spend the Dragon Boat Festival with my family soon. After the summer vacation, you can spend time with your family. Try reviewing in a different environment, wash up and go to the library.
Introduction and configuration of sudo
When the yum tool was introduced before, ordinary users need sudo to escalate the command to download or uninstall the software. So what is sudo? How to configure sudo? Let's listen to my introduction below.
What is sudo?
sudo is a command in the Linux/Unix operating system, which is used to execute commands or access system resources as a super user (that is, the root user). Generally speaking, ordinary users cannot access certain system resources or execute certain system commands, and using the sudo command can temporarily elevate user privileges to perform these operations. To use the sudo command, you need to enter the password of the current user to confirm your identity.
Phenomenon without sudo permissions
configuration of sudo
The first step is to open the sudoers configuration file with the root account
vim /etc/sudoers
The second step is to copy and paste the information in the root column and change the user name to the user you want to configure, and then write in the low line mode.
Check it out below.
Use of gcc/g++ compiler
gcc/g++ basic use
gcc 源文件.c -o
g++ 源文件.cpp/源文件.c -o
-o is followed by the name of a custom program, such as gcc source file.c -o mytest, then the compiler will not name the executable program with the default a.out after generating the executable program, but use mytest as the name of the executable program.
After g++ compiles the cpp file, the executable program generated by default is also a.out. This shows that gcc and g++ are of the same origin. So can gcc compile cpp files? Can g++ compile c files?
Conclusion: gcc cannot compile source files with .cpp suffix.
Conclusion: g++ can compile c source files with .c suffix. However, it is usually recommended to use gcc to compile source files with the .c suffix.
Program translation process
The translation process of the program is roughly divided into four types, namely preprocessing, compiling, assembling and linking. Let me briefly introduce these four compilation processes to you.
gcc preprocessing process
Preprocessing is responsible for processing the source code. At this point, the following tasks will be completed: header file expansion , macro replacement , comment deletion , and conditional compilation . After processing the above work, the compiler will generate a new text file with .i as the extension. Below I will give a simple demonstration under the gcc compiler in the Linux environment.
gcc -E 源文件.c -o 新的文本文件.i
The -E option here actually tells the compiler that you translate my source code and stop after the preprocessing work is completed.
Since we did not define the DEBUG macro, after conditional compilation, the .i file is printf("release\n"); this line of code. Since gcc can perform a series of text operations, corresponding macro definitions can also be performed. Here is a simple demonstration
gcc -E test.c -o test.i -DDEBUG
What is the use of conditional compilation? Here we are going to talk about everyone's different positioning for some software. Like the vs we use, most of the student groups generally use the community edition, and some working developers usually use the professional edition. Usually, the community edition only gives some of the most basic development packages and development tools. The professional version will provide richer and updated development kits and development tools. Will the vendor that provides these two different versions maintain two copies of the code? The answer is no, because it is enough to maintain the professional version of the code, and the tailoring of the code through conditional compilation has achieved the castration of the corresponding functions, making the product differentiated.
Expansion Supplement
Why can we carry out corresponding c/c++ development under windows or Linux? This is because we have installed vs20xx under windows, or our linux cloud server comes with a gcc/g++ compiler. These are the factors that allow us to develop corresponding c/c++ under different platforms and the first step. However, what I want to emphasize here is that header files and library files are also very important. The installation of library files and header files is the second step. This is because library files and header files can greatly improve our development efficiency, code reusability and maintainability. And all of us download the integrated development tool vs2019 or vs2022 in the windows environment and choose to install the corresponding development package. The essence is actually downloading library files and header files.
The following briefly introduces how to view the header file library files under Linux. View the location instruction of Linux header file library file
ls /usr/include/
We can also use vim to open the corresponding header file library file to view the corresponding code, and search in the bottom line mode.
vim string.h
gcc compilation process
The process of compilation is to let the source code of the high-level programming language we write be processed by the compiler into assembly code. Assembly code is made up of a series of symbolic instructions, each of which represents a machine language instruction. Let's take a brief look at how gcc converts our source code into assembly.
gcc -S test.c -o test.s
Assembly language is the underlying language of the computer, and it is also an independent discipline. Assembly language is the direct control language of computer hardware. It corresponds to machine language one by one, and one assembly instruction corresponds to one machine instruction. Some instructions like the call instruction and movl instruction in the figure above are essentially performing addressing operations or calling operations based on addresses. We don't need to study in depth, as long as we can get a general understanding.
gcc assembly process
The process of assembly is that the compiler converts the assembly code into binary instructions that the machine can recognize. The following is a simple demonstration of the assembly operation with gcc.
gcc -c test.c -o test.o
``
It can be seen that although the .o file is given executable permission, it still cannot be executed. This is because it is not linked and cannot be executed.
The linking process of gcc
Linking in the process of program translation refers to the process of merging the object code and library files generated by the compiler into executable files. The linking process is also the last stage of program translation. After linking, the program can be executed. The following is still a simple demonstration using gcc.
gcc test.o -o mytest
Of course, directly using gcc + source files can also directly generate the default executable program a.out. So what should I do if I can’t remember the above options? Here is a memory technique. Let’s look at the ESC key in the upper left corner of the keyboard, corresponding to the -E option for preprocessing, the -S option for compilation and the -c option for assembly. Note that c is lowercase and the other two options are uppercase. The corresponding files are .i, .s, and .o. You can remember that it is the International Standards Organization ios.
Introduction to the library
In the process of introducing the compiler to translate the program above, it is introduced that the header file and library file will be combined into an executable program during the linking process of the compiler. What is a library file? Below I will give a brief introduction.
When we write c language code, it is inevitable to use printf output and scanf input. These two library functions are the function methods provided by the c standard library. Of course, the essence of the library is also a file. In Linux, the suffixes of .so (dynamic library) and .a (static library) are all represented as library files. In Windows, files with the suffix of .dll (dynamic library) and .lib (static library) are called library files. Let's take the Linux environment as an example to briefly show you the storage location and naming rules of the library files.
ls /lib64/libc.so*
Methods are implemented in libraries. The library is actually a certain translation of different source files (.c files), and then packaged. In this way, only one library file needs to be provided for users to use, and the implementation of the source file can be hidden. The emergence of library files gives us the opportunity to program on the shoulders of giants, without the need to build wheels to implement some commonly used library functions. Of course, we still need to build wheels moderately when learning. So the executable program we wrote = library file + header file + source code we wrote.
library link
There are two types of library links: static link and dynamic link. The following is a brief introduction to these two links.
First introduce dynamic linking, dynamic linking requires dynamic libraries, and dynamic libraries are like Internet cafes that we used to go to when we were in school. More or less, everyone secretly went to Internet cafes to play hackers because they didn't support playing games at home. At this time, we can always find corresponding Internet cafes and friends to open black according to our needs. This is our dynamic link. One day, the Internet cafe was blocked. We big guys can't go online. This corresponds to the fact that many programs that depend on the dynamic library cannot run normally after the dynamic library is missing. This is because the compiler copies the address of the method to our target program when performing dynamic linking. Once the dynamic library is missing, it will definitely cause a program error.
We all have laptops more or less in college. Our act of buying a notebook is similar to static linking. The notebook manufacturer is similar to the role of the static library. When we have bought a laptop, but the manufacturer has closed down (the static library is missing), we can still use our laptop normally. When the compiler performs static linking, it will copy the corresponding method to our target program, so that the target program no longer depends on the static library.
simple demo link
The following briefly demonstrates the dynamic link.
gcc xxx.c -o xxx
ldd xxx
You can see that the compiler is linked by dynamic link by default. Is it okay if we want to do static linking? The answer is no problem.
gcc xxx.c -o xxx -static
The file command can be used to see how the program is linked
file 可执行程序
Expansion Supplement
The following expanded content is just an introduction, and related verifications will be provided to you one by one in the future.
1. If there is no static library, can it be statically linked? The answer is no.
2. If there is no dynamic library, but there is a static library and gcc can find the static library, can it be dynamically linked? The answer is yes. Because the essence of -static is to change the priority.
3. In daily life, the applications we use must not be completely static links or dynamic links, but a mixture of the two.
Advantages and disadvantages of dynamic and static libraries
The advantages of dynamic libraries are high resource utilization (hard disk resources, memory resources, network resources, etc.), and the disadvantage is that once the library is missing, all programs that depend on the library cannot run normally.
The advantage of the static library is that each application does not depend on the library, and the disadvantage is that the utilization rate of resources (hard disk resources, memory resources, network resources, etc.) is low.
debug&&release
The executable program compiled and generated by gcc by default is the release version. The following briefly introduces a command to release the executable program generated by gcc as the debug version.
gcc xxx.c -g
Compared with the release version, the debug version adds tracking and debugging information, so it is larger in size. This is what we have already introduced when we were studying VS2019IDE, so here is a brief mention.