Hello World
1.
2.GDB
Compile-time plus-G, the resulting executable file can be debugged with GDB
$GCC-G main.c-o main//-g option is to include the source code information in the executable file
$gdb Main
(GDB) Help//can view the category of the command
(GDB) Help files//can see what specific commands are in a category
(GDB) List 1//Start listing source code from the first line many common commands for GDB are abbreviated, such as the list command can be written as L
GDB provides a handy function to directly enter the prompt to repeat the previous command with the appropriate parameters.
(GDB) Quit//exit GDB Environment
$GDB main//debug main
(GDB) Start//Begin execution of the program
(GDB) Next//shorthand for N control these statements are executed in one line
(GDB) Step/abbreviation s enter function to execute
(GDB) Backtrack//shorthand for BT, you can view the stack frame of the function call
(GDB) Info locals//You can view the value of a local variable with the info command (abbreviated to i)
(GDB) Frame 1//First use the Frame command (abbreviated as f) to select the 1th stack frame and then view the local variables
(gdb) Print sum//Then use the Print command (p) to hit the value of the variable sum
(GDB) Finish//You can use the finish command to keep the program running until you return from the current function
(GDB) Continue//With the Continue command (abbreviated as C) running continuously rather than stepping, the program arrives at the breakpoint and automatically stops
(GDB) Break 9 if sum! = 0//Conditional breakpoint
3.$ gcc main.c stack.c-o main/One-step compilation
$ gcc-c MAIN.C//Multi-Step compilation
$ gcc-c MAIN.C
$ gcc-c stack.c$ gcc main.o stack.o-o Main
$ gcc-c main.c-wall//compiling with-WALL option MAIN.C
$GCC-C main.c-istack//compile. Use the-I option to tell the GCC header file to be found in the subdirectory stack.
4.#, # #运算符和可变参数
In a functional macro definition, #运算符用于创建字符串, #运算符后面应该跟一个形参 (spaces or tabs can be in the middle), for example:
#define STR (s) # SSTR (Hello World)
After preprocessing with the CPP command is "Hello World", automatically using "number to enclose the argument as a string, and multiple consecutive whitespace characters in the argument are replaced with a space
In the macro definition you can use # #运算符把前后两个预处理Token连接成一个预处理Token, and the # operator, # #运算符不仅限于函数式宏定义, variable-type macro definitions can also be used.
#define CONCAT (A, b) a# #b
CONCAT (Con, cat)//Pre-processing is CONCAT.
#define Hash_hash # # # # #//To define a macro to expand into two # numbers, you can define this:
In the middle of the # #是运算符, when the macro expands, the front and back two # numbers are concatenated by this operator. Note that the middle of the two spaces is not rare, if written # # #, will be divided into # #和 # #两个Token, and by definition # #运算符用于连接前后两个预处理Token, cannot appear at the beginning or end of the macro definition
Functional macro definitions can also have variable parameters, also in the parameter list ... Represents a mutable parameter
5.Makefile
- Write a makefile file and place the source code in the same directory:
MAIN:MAIN.O STACK.O MAZE.O
GCC main.o stack.o Maze.o-o Main
MAIN.O:MAIN.C main.h stack.h maze.h gcc-c main.c
STACK.O:STACK.C stack.h main.h gcc-c stack.c
maze.o:maze.c maze.h main.h gcc-c maze.c
Main is the target of this rule, MAIN.O, STACK.O, and MAZE.O are the conditions of this rule (prerequisite). The relationship between the target and the condition is: to the new target, all its conditions must be updated first, and the target must be updated as soon as one of the conditions is updated.
The so-called "update" is the execution of the rules in the list of commands, each command in the list must start with a tab, note that it cannot be a space, the format of makefile is not as random as the C//indentation
- Then run make compile in this directory
The $ make//make command automatically reads the makefile file in the current directory
Gcc-c MAIN.C
Gcc-c STACK.C
Gcc-c maze.c
GCC main.o stack.o Maze.o-o Main
For each command that starts with tab in Makefile, make creates a shell process to execute it
- 1. Attempt to update the target of the first rule in makefile Main, the goal of the first rule is called the default target, as long as the default target is updated, even if the task is completed, other work is done for this purpose. Since we are compiling for the first time, the main file has not yet been generated and obviously needs to be updated, but the rules say that the three conditions of MAIN.O, STACK.O, and MAZE.O must be updated before main can be updated.
- 2. So make will further look for rules that target these three conditions, which are not generated and need to be updated, so they are updated with the corresponding commands (gcc-c main.c, Gcc-c stack.c, and Gcc-c maze.c).
- 3. Finally execute GCC main.o STACK.O maze.o-o main update main.
Usually makefile will have a clean rule that clears the binaries generated during compilation and preserves the source files
6.
const int *a;
int const *A; The two are the same, a is a pointer to the const int, the memory unit pointed to by a is not rewritable
int * const A; A is a const pointer to the int type, *a can be rewritten, but a does not allow rewriting
int const * const A; A is a const pointer to the const int type, so neither *a nor a is allowed to overwrite.
7.
The umask mask for the $umask//shell process can be viewed with the umask command
$umask 0//We can change the shell process's umask to 0
$ Touch file123$ ls-l file123-rw-r--r--1 akaedu akaedu 0 2009-03-08 15:07 file123
When you create a file with the Touch command, the Create permission is 0666, and the touch process inherits the Umask mask of the shell process, so the final file permission is 0666&~022=0644.
8.GCC Common Options
-C
Compiling the build target file
-E
The CPP command can achieve the same effect only if it is preprocessed and not compiled
-G
Adding debug information to the generated target file, the so-called debug information is the correspondence between the source code and the instruction, which is used when gdb debugs and Objdump disassembly.
-idir
Dir is the directory where the header file resides
-ldir
Dir is the directory where the library file resides
-M and-mm
Output ". o file:. c file. h file" This form of makefile rule, the output of-mm does not include the system header file
-O outfile
outfile filename of the output file
-O?
Various compilation optimization options
-V
Print a detailed compilation link process
-wall
Print all the warning messages
"Linux C Programming One-stop learning"-notes