Table of contents
3. Global functions and static functions
Fourth, the compilation process (understand)
Five, the header file contains
7. The difference between macros with parameters and functions with parameters
8. Conditional compilation/code pruning
Nine, to prevent repeated inclusion of header files
1. Conditional compilation method (recommended)
10. Dynamic library and static library
1. The library and project are in the same directory
2. The library and the header files of the library are in the custom directory
3. The library and library header files are in the system directory
1. The library and project are in the same directory
2. The library and the header files of the library are in the custom directory
3. The library and library header files are in the system directory
1. Memory partition
On a 32-bit platform, each process occupies 4G space (virtual space)
memory partition | nature | use and storage |
---|---|---|
heap area | readable and writable | Use malloc, calloc, realoc, free to apply dynamically |
stack area | & can read and write | Local variables, formal parameters of functions, and return values>4B |
Global Zone/Static Zone | & can read and write | Ordinary global variables, static global variables, static local variables |
literal constant | read only | Numeric constants, character (string) constants, symbolic constants (array names) |
code area | read only | code binary instructions |
2. Variables
1. Ordinary local variables
Definition:: ordinary variables defined inside {}
Scope of action: effective between the {} compound statements where it is located
Life cycle: effective between the {} compound statements where it is located
Storage area: stack area
Precautions:
1. Ordinary local variables are not initialized and the content is uncertain
2. The principle of common local variables with the same name and proximity (try to have different names)
2. Ordinary global variables
Definition: ordinary variables defined outside the function
Scope: all source files
Life cycle: the whole process.
Storage area: global area
Precautions:
1. Global variables are not initialized to 0
2. When the global variable and the local variable have the same name, the local variable is preferred.
3. When other source files use global variables or functions, they must be declared extern. extern declares a variable or function from outside, by compilation.
1.c
2.c
Note: Both need to be compiled, not just one file
3. Static local variables
Definition: Local variables defined in {} plus static
Scope of action: effective between the {} compound statements where it is located
Life cycle: the entire process is valid, that is, the variable will not disappear
Life cycle: valid for the entire process
Note: 1. Static local variables are not initialized to 0
2. Static local variables exist throughout the process
4. Static global variables
Definition: variables defined outside the function with static modification
Scope of application: It can only be used in the current source file , and cannot be used in other source files.
Life cycle: the whole process
Storage area: global area
Note: 1. Static global variables are not initialized to 0
2. Static global variables can only be used in the current source file
3. Global functions and static functions
1. Global functions (functions are global functions by default)
Global functions: can be used in the current source file and other source files
If other source files use extern to declare global functions
2. Static function (function with static modification)
Static functions can only be used in the current source file
Form: static [return value type] function name (parameter list){...}
Fourth, the compilation process (understand)
Compilation process: preprocessing, compiling, assembling, linking
Take the hello.c file as an example
Preprocessing: macro replacement, comment deletion, header file inclusion, conditional compilation -E (no syntax error will be reported)
gcc ‐E hello.c ‐o hello.i
Compile: Compile the preprocessed file into an assembly file/assembly instruction (report syntax error)
gcc ‐S hello.i –o hello.s
Assembly: Generate binary files from assembly files
gcc ‐c hello.s ‐o hello.o
Link: generate executable file from project binary file + library function + startup code
gcc hello.o -o [generate executable file name]
complete in one step:
1 gcc main.c ‐o main
2 gcc main.c
Five, the header file contains
It is recommended to include system header files in #include<head.h> <>
<>Find the head.h header file from the specified directory of the system
#include "head.h" "" is recommended to include custom header files
"" First look for the head.h header file from the current directory, if you can't find it, then look for it in the directory specified by the system
6. #define macro
Example: #define PI 3.14 (macro definition)
Use 3.14 in the preprocessing stage to replace all occurrences of PI (macro expansion)
Note: Do not add a semicolon after the macro, the macro should be capitalized as much as possible to distinguish it from ordinary variables
Macro scope of action: it is valid from the beginning of the definition to the end of the current file.
Generally, you cannot cross files. Cross-files need to put the macro definition in the header file.
#undef can end the scope of the macro
Macros have no attribution, i.e. are not bounded by the compound statement {}.
1. Macros without parameters
1 #define PI 3.14
2 #define STR "hello world"
3 #define N 100
2. Macros with parameters
Take multiplication as an example:
#define MY_MUL(a, b) a*b
printf("%d\n", MY_MUL(10,20) );//10*20//printf("%d\n", 10*20 );
Note: 1. The parameters of the macro cannot have a type
#define MY_MUL(int a, int b) a*b //wrong way of writing
2. The macro cannot guarantee the integrity of the parameters
#define MY_MUL(a, b) a *b
printf("%d\n", MY_MUL(10, 20)); //10*20
printf("%d\n", MY_MUL(10 + 10, 20 + 20)); //10 + 10*20 + 20 == 230
The form of () can be used to make the macro with parameters have certain integrity: #define MY_MUL(a, b) ((a) *(b))
printf("%d\n", MY_MUL2(10 + 10, 20 + 20));//((10 + 10) * (20 + 20))== 800
7. The difference between macros with parameters and functions with parameters
The macro with parameters will be expanded several times when it is called several times. There is no function call process when executing the code, and there is no need to push and pop the stack
Therefore, macros with parameters waste space and save time, and functions with parameters waste time and save space.
8. Conditional compilation/code pruning
Good for code clipping.
3 statement types:
Take character case conversion as an example:
If you define a macro:
Judging whether it is established:
Nine, to prevent repeated inclusion of header files
1. Conditional compilation method (recommended)
#ifndef __05_A_H__
#define __05_A_H__
/* code */
int num=10; 5 6 #endif
2. Windows mode
#pragma once
/* code */
10. Dynamic library and static library
Library: Generate binary files from source files and only need to link to generate executable files
The static library link phase imports all functions of the static library into the project
Advantages: less dependence on the library
Disadvantage: the generated executable file is large
Generate a static library under linux: sudo gcc test.c (with a custom header file) -o main01 (main01 is the static library)
The dynamic library link phase only establishes the relationship of the dynamic library related functions, and the running phase imports its binary.
Pros: The resulting executable is small
Disadvantages: large dependence on the library
Generate a dynamic library under linux: sudo gcc test.c (there is a custom header file) -o main02 (main02 is the dynamic library)
11. Make a static library
gcc ‐c fun.c ‐o fun.o
ar rc libtestlib.a fun.o
The static library libtestlib.a starts with lib. A ends with the name of the library testlib in the middle
Static library use:
1. The library and project are in the same directory
sudo gcc main.c (custom header fun.h including fun.c) libtestlib.a
2. The library and the header files of the library are in the custom directory
sudo gcc main.c ‐I./lib ‐L./lib ‐ltestlib
‐I (capital i) followed by the path to the header file
‐L followed by the path of the library
‐l (lowercase L) followed by the name of the library (name between lib and .a)
3. The library and library header files are in the system directory
Copy the library to /usr/lib, and copy the header files to /usr/include
Then execute sudo gcc main.c -ltestlib (given the library name)
12. Make a dynamic library
gcc ‐shared fun.c ‐o lib testlib .so (the red part is the library name)
Dynamic dynamic library use:
Compile link: sudo gcc main.c -o main libtestlib.so
1. The library and project are in the same directory
Adding the current path ./ to the library's search directories works fine
export LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH ($ takes the value of this variable) (add multiple paths separated by colons)
Then you can directly execute ./main compilation
2. The library and the header files of the library are in the custom directory
Add the current path ./lib to the search directories for libraries
export LD_LIBRARY_PATH=./lib:$LD_LIBRARY_PATH
3. The library and library header files are in the system directory
Compile link: sudo gcc main.c -o main -ltestlib
(If the static library and dynamic library testlib with the same name exist in the system directory at the same time,
By default, the dynamic library is selected for compilation, and the static link can only be linked with the -static modification. )
The library is in the system directory /usr/lib and the header file is in /usr/include, which can be run directly