[Linux] 5. Use of development tools (gcc/gdb/makefile)

Linux compiler - gcc/g++ use

1. Review the compilation and linking process

insert image description here

2. Understand the meaning of the options

insert image description here
insert image description here
insert image description here
These options are hard to remember, what should I do? – ESc - iso (corresponding)

insert image description here

gcc-options

  • -E only activates preprocessing, this does not generate files, you need to redirect it to an output file
  • -S compile to assembly language do not assemble and link
  • -c compile to object code
  • -o file output to file
  • -static This option uses static linking for the generated files
  • -g Generate debugging information. The GNU debugger can use this information.
  • -shared This option will use the dynamic library as much as possible, so the generated file is relatively small, but the system needs the dynamic library.
  • -O0
    -O1
    -O2
    -O3 4 levels of compiler optimization options, -O0 means no optimization, -O1 is the default value, and -O3 has the highest optimization level
  • -w Do not generate any warning messages.
  • -Wall Generate all warning messages.

3. Understanding of dynamic and static links

First of all, we need to be clear: the code we implement ourselves and the code in the library are two concepts. The
C standard library has been compiled by others and provided for us to use. When we use the code (printf) in the library, we are just ourselves. The interface of the function call is written, and there is no corresponding implementation. Only when linking, the corresponding implementation is associated with the code we compiled!
So the essence of the link: how to establish an association with the standard library when calling the library function?
insert image description here
insert image description here
insert image description here
insert image description here
In order to support compilation, what does the system provide to users?
The system provides us with the .h of the standard library (tells us how to call the function) and the standard dynamic and static library .so/.a (tells the location of the linker function)

User code + library code <==> executable program

Are these conclusions only valid under the Linux system?
The principle under Windows is the same, except that the dynamic library under Windows is .dll, and the static library is .lib.
The executable programs formed by default are all dynamically linked.

Linux project automation build tool-make/Makefile

1. Background

  • Whether you can write a makefile, from one side shows whether a person has the ability to complete large-scale projects
  • The source files in a project are not counted, and they are placed in several directories according to type, function, and module. The makefile defines a series of rules to specify which files need to be compiled first, which files need to be compiled later, and which files need to be re-compiled. Compile, and even perform more complex functional operations
  • The benefit brought by makefile is - "automatic compilation". Once written, only one make command is needed, and the entire project is completely automatically compiled, 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++, 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 automatic construction of the project.

2. use

insert image description here

3. Understand the grammar rules

insert image description here
insert image description here
insert image description here

4. Understand the derivation rules of makefile

insert image description here

Use of Linux debugger-gdb

Debugging is an indispensable part of code writing.
Debugging under windows is through F9 F10 F10 and the visual window for debugging
, but what about under Linux? It is to debug through the command line.
The debugging ideas of the two are the same, but the operation methods are different.
insert image description here
The code of mytest.c

1. gdb XXX

insert image description here
insert image description here
But why the content of the display file cannot be displayed? ?
Answer:By default, gdb cannot debug programs that are released nowIn Linux, the release version is the default when generating software with gcc/g++
insert image description here

2. Instructions in debug mode

2.1 l (list) display code

insert image description here

2.2 Add/delete/view breakpoints

insert image description here

2.3 Commissioning phase

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/weixin_60915103/article/details/130446993