Embedded technology study notes (3)

Linux development tool
vim (text editor): learn to use and how to configure vim
gcc (compiler): learn to make dynamic and static libraries and use and distinguish
gdb (debugger): set breakpoints, single-step execution, debug memory errors
make (Project Manager): write makefile
vim using
vim is the most powerful text editor, its purpose is to break away from the mouse.
The configuration files required by vim are entered one by one according to your own purpose, and the contents set are the ones that best meet your own requirements. So many people like to use vim, enter vim / etc / vim / vimrc to enter the configuration file.
First of all, if you want to use vim, enter sudo apt-get install vim in ubunte to get vim, and then enter vim plus edit file name to edit . It is not possible to enter any characters just after entering vim, you need to press the i key to enter the input mode, and then you can edit it. If you want to exit, first press esc, then press shift +: enter the bottom line mode, and then enter the corresponding characters to achieve the proper function, where w is to save, q is to exit the
command line mode (you can enter yy to copy a line of content , Enter p to paste, enter dd to delete a line)
Insert mode (enter i or a to enter)
bottom line mode (hold shift +: to enter)
gcc uses
gcc is the compiler, full name is GNU cc
1, gcc features
gcc GNU tool is a cross compiler. It refers to a cross compiler to compile the current CPU platform executable in the CPU other platform code, or target code (mobile phones, the development of plate) can run on a host name (pc) compile
desired pretreatment What to do: header file expansion, macro replacement, conditional compilation
Use the difference between <> and "" after include. The search path of <> is the system's header file directory (/ usr / include). The search path of "" is the current directory search. If you cannot find it, enter the system's header file directory. .
What to do for compilation: Compile the preprocessed file into an assembly file
. What to do for assembly: Compile the assembly file to generate the target file. O What
to do for linking: Create a symbol table and generate an executable file

gcc common compilation options After the
gcc command + -E means only pre-processing
gcc command + -I means to specify a third-party search path, used to search for header files when the
gcc command +> means to save the output content again Elsewhere
after the gcc command + -D means to define the macro
gcc command + -S means to stop the
gcc command at the completion of compilation + -o means to rename the executable file
gcc command + -w means to turn off the warning message during compilation, but This instruction is not recommended. Sometimes it will close some information that affects the execution of the program
after the gcc command + -Wall opens the warning message during compilation.
After the gcc command + -O1 (-O2, -O3) means compiler optimization
2, static library and dynamic Library production
: Library files: Saved functions and variables Features: Saved functions and variables, can only be used but can not see their implementation
Static library: Loaded during the compilation phase, loading refers to loading the code of the library file into the source file, with the suffix. a
dynamic library: loaded at runtime. The suffix is ​​.so

There are differences between the executable files formed by these two libraries:
(1) The executable file generated by the static library is larger than the executable file generated by the dynamic library, resulting in more memory used by the program to run
(2) The executable file generated by the static library The file is difficult to upgrade, and the executable file generated using the dynamic library is easy to upgrade.
(3) Executable files generated using static libraries run fast and executable files generated using dynamic libraries run slowly
(4) Executable files generated using static libraries are easy to deploy, and executable files generated using dynamic libraries are difficult to deploy

Linux library file storage path
/ lib: store the library required for system operation
/ usr / lib: store the library required for program operation

How to make a static library
(1) Compile the file where the function needs to be saved to generate a target file
(2) Static library production tool: ar ar rcs (create it if it does not exist, update it if it exists) Library name target name
(3) Use static library : Gcc source file-I library name-L stock path
How to make dynamic library
gcc -shared -fPIC source file -o libcal.so
Use: cp libcal.so / usr / lib
gcc main.c -lcal
gcc main.c /libcal.so -o cal

The role of gcc -static
This option will prohibit the use of dynamic libraries, so the compiled things are generally very large, and do not need any dynamic link library to run.

Compiler three-level optimization
1, compiler optimization level 1
performs the optimization of the basic code at the first level of optimization. This level attempts to perform 9 separate optimization functions:
-fdefer-pop: This optimization technique is related to how the assembly language code operates when the function is completed. In general
, the input value of a function is stored on the stack and accessed by the function. When the function returns, the input value is still on the
stack. In general, after the function returns, the input value is immediately popped from the stack. Doing so will
clutter the contents of the stack .
-fmerge-constans: With this optimization technique, the compiler tries to merge the same constants. This feature
sometimes results in a long compilation time, because the compiler must analyze each constant used in the c or c ++ program,
and Compare them with each other.
-Fthread-jumps: The use of this optimization technique is related to how compilers deal with conditional and unconditional
branches in assembly code . In some cases, a jump instruction may be transferred to another branch statement. Through a series of
jumps, the compiler determines the final target between multiple jumps and redirects the first jump to the final target.
-floop-optimize: By optimizing how to generate loops in assembly language, the compiler can
improve the performance of the application on very large programs . Usually, the program consists of many large and complex loops. By deleting
variable assignment operations that do not change values ​​within the loop, the number of instructions executed within the loop can be reduced, and performance can be greatly improved
. In addition, conditional branches that determine when to leave the loop are optimized to reduce the impact of the branch.
-fif-conversion: The if-then statement should be the most time-consuming part of the application after the loop.
A simple if-then statement may produce numerous conditional branches in the final assembly language code. The
compiler
can reduce the amount of time spent in if-then statements by reducing or deleting conditional branches, and using conditional transfer to set flags and using arithmetic techniques to replace them .
-fif-conversion2: This technique combines more advanced mathematical features to reduce
the conditional branches needed to implement if-then statements .
-fdelayed-branch: This technique attempts to rearrange instructions based on instruction cycle time. It also attempts to
move as many instructions as possible before the conditional branch in order to make the most of the processor's governance cache.
-fguess-branch-probability: As the name implies, this technique attempts to determine the most
likely result of conditional branching , and the corresponding move instruction, which is similar to the delayed branching technique. Because the code arrangement is predicted at compile time,
using this option to compile the same C or C ++ code twice is likely to produce different assembly language code, depending
on which branch the compiler thinks will use it at compile time. For this reason, many programmers do not like to use this feature, and
specifically use the -fno-guess-branch-probability option to turn off this feature
-fcprop-registers: because the register is assigned to the variable in the function, the compiler performs the second Check in order to reduce
scheduling dependencies (both segments require the same register) and delete unnecessary register copy operations.
2, compiler optimization level 2
Combining all the optimization techniques of the first level, plus some optimizations:
-fforce-mem: This optimization forces all variables stored in the memory location to be copied into registers before any instruction uses the variables
. For variables that involve only a single instruction, this may not have a great optimization effect. But for
variables that are involved in many instructions (must be mathematical operations) , this will be a significant optimization, because and access to memory Compared with the value in, the processor accesses the value in the register much
faster.
-foptimize-sibling-calls: This technique handles related and / or recursive function calls. In general, recursive function calls
can be expanded into a series of general instructions instead of using branches. In this way, the instruction cache of the processor can load the expanded instructions and process them, which is faster than keeping the instructions as separate function calls that require branch operations.
-fstrength-reduce: This optimization technique optimizes the loop and deletes iteration variables. Iteration variables are variables that are tied to the loop counter
, such as using variables, and then using the loop counter variables to perform a mathematical for-next loop.
-fgcse: This technique performs a global expression elimination process on all generated assembly language code. These optimization operations attempt to analyze the
generated assembly language code and combine common fragments to eliminate redundant code segments. If the code uses computational goto, the gcc instruction recommends the -fno-gcse option.
-fcse-follow-jumps: This special general-purpose subexpression elimination technique scans the jump instruction to find the target code in the program that will not be reached by any other means. The most common example of this is the else part of the if-then-else statement.
-frerun-cse-after-loop: This technique reruns the general subexpression elimination routine after any loop has been optimized.
This ensures that the code is further optimized and coded after the loop code is unfolded.
-fdelete-null-pointer-checks: This optimization technique scans the generated assembly language code for code that checks for null pointers. The compiler
assumes that indirect reference to a null pointer will stop the program. If you check the pointer after an indirect reference, it cannot be null.
-fextensive-optimizations: This technique implements various optimization techniques that are costly from a compile-time perspective, but it may
have a negative impact on runtime performance.
-fregmove: The compiler attempts to reallocate the registers used in the mov instruction and use them as operands for other instructions in order to maximize
the number of bound registers.
-fschedule-insns: The compiler will try to rearrange the instructions in order to eliminate processors waiting for data. For processors with delays when performing floating-point operations, this allows the processor to load other instructions while waiting for floating-point results.
-fsched-interblock: This technique enables the compiler to schedule instructions across instruction blocks. This can move instructions very flexibly to maximize the work done during the waiting period.
-fcaller-saves: This option instructs the compiler to save and restore registers for function calls, so that functions can access register values ​​without saving and restoring them. If you call multiple functions, this saves time because you only need to save and restore the register once, rather than in every function call.
-fpeephole2: This option allows any computer-specific observation hole optimization.
-freorder-blocks: This optimization technique allows instruction blocks to be rearranged to improve branch operations and code locality.
-fstrict-aliasing: This technique enforces strict variable rules for high-level languages. For C and C ++ programs, it ensures that
variables are not shared between data types. For example, integer variables do not use the same memory location as single-precision floating-point variables.
-funit-at-a-time: This optimization technique instructs the compiler to read the entire assembly language code before running the optimization routine. This allows the compiler to rearrange code that does not consume a lot of time in order to optimize the instruction cache. However, this will consume a considerable amount of memory at compile time, which may be a problem for small computers.
-falign-functions: This option is used to align functions to the beginning of specific boundaries in memory. Most processors read memory according to pages, and ensure that all function codes are in a single memory page, so there is no need to call the pages required by the code.
-fcrossjumping: This is the conversion code processing of cross jumps, so as to combine the same code scattered in the program. This can reduce the length of the code, but may not have a direct impact on program performance.
3. Compiler optimization level 3
It integrates the left and right optimization techniques in the first and second levels, and also includes the following optimization:
-finline-functions: This optimization technology does not create a separate assembly language code for the function, but puts the function The code is included in the code of the scheduler. For functions that are called multiple times, copy the function code for each function call. Although this is detrimental to reducing the code length, performance can be improved by making the most of the instruction cache code instead of branching at each function call.
-fweb: Build a pseudo register network for storing variables. Pseudo-registers contain data as if they were registers, but they can be optimized using various other optimization techniques, such as cse and loop optimization techniques.
-fgcse-after-reload: This technique performs a second gcse optimization after completely reloading the generated and optimized assembly language code to help eliminate any redundant segments created by different optimization methods.

Published 14 original articles · Like1 · Visits 477

Guess you like

Origin blog.csdn.net/a1152946932/article/details/104773112