Linux C language development tools-learn to use gcc, gdb and make/Makefile tools through the progress bar small program

1. Implementation of progress bar program under Linux

The code to implement the progress bar is very simple, the code is as follows

#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define NUM 101

int main()
{
    
    
  int i = 0;
  char bar[NUM]; //用于存放进度条数组
  memset(bar, 0, sizeof(bar));
  char label[] = "|/-\\";

  for(i = 0; i < 100; ++i)
  {
    
    
    bar[i] = '#';
    printf("[%-100s][%3d%%][%c]\r", bar, i+1, label[i%4]);
    fflush(stdout);
    usleep(10000);
  }
  printf("\n");
  return 0;
}

The final result
Insert picture description here
If you want to compile the above code, you need to master the basic development tools under Linux

Two, Linux compiler-gcc

A C language program needs to go through four steps: preprocessing, compilation, assembly, and linking.
How does gcc complete these four steps? The format of gcc under Linux is as follows:

gcc [options] file to be compiled [options] [object file]

2.1 Preprocessing (for macro replacement)

  • The preprocessing function mainly includes macro definition, file inclusion, conditional compilation, de-comment and so on .
  • Preprocessing instructions are lines of code beginning with the # sign.

Take our progress bar code for example

gcc -E proc.c -o proc.i

  • Option"-E", the function of this option is to make gcc stop the compilation process after the preprocessing is over .
  • Option"-The"Refers to the target file, and the " .i" file is the C original program that has been preprocessed .

Insert picture description here

2.2 Compile (generate assembly)

In this stage, gcc must firstCheck the standardization of the code, whether there are grammatical errors, etc.To determine the actual work to be done in the code, inAfter checking it is correct, gcc translates the code into assembly language

Give a chestnut:

gcc -S proc.i -o proc.s

  • Option"-S", the function of this option is to stop gcc to generate assembly language after compilation .
  • Option"-The"Refers to the target file, and the " .s" file is the original C program that has been compiled .

Insert picture description here

2.3 Assembly (generate machine identifiable code)

The assembly stage is to convert the ".s" file generated in the compilation stage into an object file

Give a chestnut

gcc -c proc.s -o proc.o

  • Option == "-c" == you can see that the assembly code has been converted to binary object code of ".o", and you can see that the binary file will display garbled characters if it is opened in a normal way

Insert picture description here
You can view the binary file with the nm command
Insert picture description here

2.4 Link (generate executable file or library file)

After successful compilation, it enters the linking phase. We can generate the executable file we want

for example

gcc proc.o -o proc

Insert picture description here

2.5 Static and dynamic libraries

  • In our C program, andThe function implementation of "printf" is not defined, And in the "stdio.h" included in the precompilation, there is only the declaration of the function, but the implementation of the function is not defined. Then, where is the "printf" function implemented?

the answer is:

The system implements these functions in a library file named libc.so.6, When there is no special designation, gcc will search under the system default search path "/usr/lib", which isLink to libc.so.6 library function, So that the function "printf" can be implemented, and this isThe role of links

Function libraries are generally divided into static libraries and dynamic libraries.

  1. Static library means that when compiling and linking, all the code of the library file is added to the executable file , so the generated file is relatively large , but the library file is no longer needed at runtime . The suffix name is generally ".a".
  2. In contrast to the dynamic library, the code of the library file is not added to the executable file when compiling and linking, but the library is loaded by the runtime link file when the program is executed , which can save system overhead . The general suffix of the dynamic library is ".so". As mentioned earlier, libc.so.6 is the dynamic library. gcc uses dynamic libraries by default when compiling. After the link is completed, gcc can generate an executable file,

2.6 gcc options

If you are familiar with the first three options, we usually use the .c file to directly generate the executable file, the effect is as follows

Insert picture description here
Other options of gcc are as follows

-E Only activate preprocessing, this does not generate a file, you need to redirect it to an output file
-S Compile to assembly language without assembly and linking
-c Compile to target code
-The File output to file
-static This option uses static linking for the generated files
-gGenerate debugging information. The GNU debugger can use this information.
-shared This option will try to use the dynamic library, so the generated file is relatively small, but the system needs the dynamic library.
-O0
-O1
-O2
-O3 There are 4 levels of optimization options for the compiler, -O0 means no optimization, -O1 means The default value, -O3 is the highest optimization level
-w does not generate any warning messages.
-Wall generates all warning messages.

Three, Linux debugger-gdb

There are two ways to release the program, debug mode and release mode
. The binary program from Linux gcc/g++, the default is release mode

3.1 gdb debugging program

Insert picture description here
To use the gdb debugger , you must generate binaries in the source code when coupled with the -g option
For chestnuts

gcc proc.c -o proc-debug -g
Insert picture description here

gdb proc-debug #开始调试程序

  • list/l line number: display binFile source code, then go to the following position from last time, 10 lines each time.
  • list/l function name: List the source code of a function.
  • r or run: Run the program.
  • quit /q: Quit gdb.

Insert picture description here

3.2 gdb uses breakpoints

break(b) Line number: set a breakpoint on a line
break function name: set a breakpoint at the beginning of a function
info break: view breakpoint information.
finish: execute until the current function returns, and then stand down and wait for the command
print (p) (p)( p ) : print the value of the expression, through the expression you can modify the value of the variable or call the function
p variable: print the value of the variable.

Insert picture description here

delete breakpoints: delete all breakpoints
delete breakpoints n: delete the breakpoint with sequence number n
disable breakpoints: disable breakpoints
enable breakpoints: enable breakpoint
info (or i) breakpoints: see which breakpoints are currently set
display Variable name: trace view A variable, its value is displayed every time it stops.
undisplay: cancel the tracking of the previously set variables

Insert picture description here
Insert picture description here

Four, Linux project automated build tool-make/Makefile

Will you write a makefile? It explains from one side whether a person has the ability to complete large-scale projects. The source files in a project are not counted. They are placed in several directories according to type, function, and module. Makefile defines a series of The rules specify which files need to be compiled first, which files need to be compiled later, which files need to be recompiled, and even more complex functions. The benefit of the makefile is-"automatic compilation". Once written, only one is needed. The make command, the entire project is fully compiled automatically, which greatly improves the efficiency of software development. make is a command tool that explains the instructions in the makefile. Generally speaking, most IDEs have this command, such as make in Delphi, nmake in Visual C++, and make in GNU under Linux. It can be seen that makefiles have become a compilation method in engineering.
make is a command, makefile is a file, and the two are used together to complete the automated construction of the project

4.1 Single file Makefile

First, build the Makefile. If you want to generate an executable file, the complete process needs to go through four steps: preprocessing, compilation, assembly, and linking, as shown in the figure below
Insert picture description here

  • Dependency The
    above file proc, it depends on proc.o
    proc.o, it depends on proc.s
    proc.s, it depends on proc.i
    proc.i, it depends on proc.c
  • The dependency method
    gcc hello.* -option hello.* is the corresponding dependency

After the build is completed, you can use the make command to complete the construction of the single-file program, and use make clean to complete the project cleanup.
Insert picture description here
Usually we use a simpler way to build a single-file project
Insert picture description here
Insert picture description here
to generate a solution. Makefile also provides project cleanup, as shown in the figure below. Shown
Insert picture description here

Cleanup command

Insert picture description here

4.2 Multi-file Makefile

We first create a multi-file proc.h

#pragma once
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define NUM 101

extern void proc();

proc.h

#include "proc.h"

void proc()
{
    
    
  int i = 0;
  char bar[NUM]; //用于存放进度条数组
  memset(bar, 0, sizeof(bar));
  char label[] = "|/-\\";

  for(i = 0; i < 100; ++i)
  {
    
    
    bar[i] = '#';
    printf("[%-100s][%3d%%][%c]\r", bar, i+1, label[i%4]);
    fflush(stdout);
    usleep(100000);
  }
  printf("\n");
}

main.c

#include "proc.h"

int main()
{
    
    
  proc();
  return 0;
}

We want to build a multi-file project as shown below, which can be written one by one.
Insert picture description here
However, if there are too many files, we will adopt the following method
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_40076022/article/details/113403676