C programming practices in Linux (1 )?? Build a development platform. GCC compiler GCC is the most important development tool on the Linux platform. it is the gnu c and C ++ compilers. its basic usage is as follows: gcc [options] [filenames] options is the compilation option. In total, GCC provides more than 10 compilation options...
Practice of C programming in Linux (1)
?? Development platform construction
. GCC compiler
GCC is the most important development tool in Linux. it is the gnu c and C ++ compilers. its basic usage is as follows:
Gcc [options] [filenames]
Options is the compilation option. GCC provides more than 100 compilation options, but only a few are frequently used. we will only introduce several common options.
Suppose we compile a program that outputs "Hello World:
/* Filename: helloworld. c */
Main ()
{
Printf ("Hello World" n ");
}
The simplest compilation method is not to specify any compilation options:
Gcc helloworld. c
It generates the default file name a. out for the target program. we can use the-o compilation option to specify a file name for the generated executable file to replace a. out. For example
The c program of helloworld. C is compiled into an executable file named helloworld. enter the following command:
Gcc-o helloworld. c
-The c option tells GCC to skip the assembly and connection steps only by compiling the source code as the target code;
The-S compilation option tells GCC to stop compilation after an assembly language file is generated for C code. The default extension of the assembly language file generated by GCC is. s. the preceding program runs the following command:
Gcc-S helloworld. c
The assembly code of helloworld. c will be generated, using AT&T assembly. Use emacs to open the assembly code, for example:
The-E option indicates that the compiler only preprocess the input file. When this option is used, the pre-processor output is sent to the standard output (the screen by default) instead of stored in the file.
The-O option tells GCC to optimize the source code to make the program run faster. the-O2 option tells GCC to generate code as small as possible and as fast as possible. Compilation speed using the-O2 option
It is slower than-O, but the generated code is faster.
The-g option tells GCC to generate debugging information that can be used by the GNU Debugger to debug your program. Fortunately, in GCC, we can use-g and-O to generate optimization code ).
-The pg option tells GCC to add additional code to your program. during execution, gprof profiling information is generated to show the time consumption of your program.
3. GDB debugger
GCC is used to compile programs, while gdb, another GNU tool in Linux, is used to debug programs. Gdb is a powerful debugger used to debug C and C ++ programs.
Debugging tasks, including setting breakpoints, viewing variables, and one step.
The most common commands are as follows:
File: load the executable file to be debugged.
Kill: terminate the program being debugged.
List: The source code is displayed in the list.
Next: execute a line of source code without entering the function.
Step: execute a line of source code and enter the function.
Run: run the program currently being debugged.
Quit: terminate gdb
Watch: monitor the value of a variable.
Break: sets a breakpoint in the code. it is suspended when the program is executed here.
Make: re-generate executable files without exiting gdb
Shell: execute shell without leaving gdb
Next we will demonstrate how to use GDB to debug a request for 0 + 1 + 2 + 3 +... + 99 programs:
/* Filename: sum. c */
Main ()
{
Int I, sum;
Sum = 0;
For (I = 0; I <100; I ++)
{
Sum + = I;
}
Printf ("the sum of 1 + 2 +... + is % d", sum );
}
Run the following command to compile sum. c (add the-g option to generate the debug information ):
Gcc-g-o sum. c
On the command line, type gdb sum and press the Enter key to start debugging sum. then run the run command to execute sum. the following content is displayed on the screen:
List command:
The list command is used to list the source code. if you run the list command twice for the above program, the following figure is displayed (the source code is marked with a line number ):
Based on the listed source program, if we set the breakpoint to 5th rows, we only need to enter the following command at the gdb command line prompt to set the breakpoint: (gdb) break 5. the execution is as follows:
At this time, we run the program again, and the program will stop at 5th rows, such:
Another syntax for breakpoint setting is break. It stops when it enters the specified function.
In contrast, clear is used to clear all defined breakpoints. Clear the breakpoint set on the function, clear Clears the breakpoint set on the specified row.
.
Watch Command:
The watch command is used to view the value of a variable or expression. to view the sum variable, you only need to run watch sum:
Watch Set an observation point for the expression (variable) expr. when a number of expression values change, the program stops running.
To view the current watch, you can use the info watchpoints command.
Next and step commands:
Next and step are used for single-step execution. during the execution, changes in the watch variable are displayed in real time (Old value and New value are displayed respectively), for example:
The difference between the next and step commands is that when a function is called by a step, it jumps to the start line of the function definition for execution, while next does not enter the function. it regards the function call statement
Execute a common statement.
4. Make
Make is a tool that must be mastered by all users who want to program on the Linux system. for any program of a relatively large scale, we will use make. it can be said that the program that does not use make does not
Any practical value.
Here, we need to explain the differences between compilation and connection. The compiler uses the source code file to generate some form of object files. during the compilation process, the external symbol reference
It is not interpreted or replaced (that is, external global variables and functions are not found ). Therefore, errors reported during the compilation phase are generally syntax errors. The connector is used to connect the target file.
To generate an executable program. In the connection phase, a reference to the symbols in other files in a target file is explained. if the symbols cannot be found, a connection error is reported.
.
The general steps of compilation and connection are as follows: in the first stage, the source files are compiled into the target files one by one, and in the second stage, all the target files are added to the required package and connected to an executable file.
. This process is very painful. we need to use a large number of gcc commands.
Make frees us from compilation and connection of a large number of source files and completes them in one step. The main task of GNU Make is to read a text file called makefile. This
Files (the target file is not necessarily the final executable program, it can be any type of file) generated by which (depending on the file, what kind of life
. Make checks the files on the disk based on the information in this makefile. if the creation or modification time of the target file is earlier than that of one of its dependent files, make executes the corresponding
Command to update the target file.
Suppose we write down the following three files. add. h is used to declare the add function. add. c provides the function bodies for adding two integers, while main. c calls the add function:
/* Filename: add. h */
Extern int add (int I, int j );
/* Filename: add. c */
Int add (int I, int j)
{
Return I + j;
};
/* Filename: main. c */
# Include "add. h"
Main ()
{
Int a, B;
A = 2;
B = 3;
Printf ("the sum of a + B is % d", add (a + B ));
};
How to generate makefile for the above three files? As follows:
-------------------------
Test: main. o add. o
Gcc main. o add. o-o test
Main. o: main. c add. h
Gcc-c main. c-o main. o
Add. o: add. c add. h
Gcc-c add. c-o add. o
-----------------------
(Note that the delimiter is the TAB key)
The makefile above uses add. c and add. execute gcc-c add. c-o add. o command to generate add. o target code, using main. c and add. execute gcc-c main. c-o
Main. o command to generate main. o target code, and finally use main. o and add. o file (the target code of the two modules) execute gcc main. o add. the o-o test command generates an executable file.
Test.
We can add variables to makefile. Environment variables are also interpreted as make variables during the make process. These variables are case-sensitive and generally use uppercase letters. Make changes
You can do many things, such:
I) stores a file name list;
Ii) storage of executable file names;
Iii) store compiler options.
To define a variable, you only need to write the name of the variable at the beginning of a row, followed by a = sign and the value of the variable. To reference a variable, write a $ symbol followed
Name ). We will rewrite the previous makefile with variables (and assume that the-Wall-O-g compilation option is used ):
OBJS = main. o add. o
CC = gcc
CFLAGS =-Wall-O-g
Test: $ (OBJS)
$ (CC) $ (OBJS)-o test
Main. o: main. c add. h
$ (CC) $ (CFLAGS)-c main. c-o main. o
Add. o: add. c add. h
$ (CC) $ (CFLAGS)-c add. c-o add. o
You can also define the clean object in makefile to clear intermediate files generated during compilation. for example, add the following code to the makefile file:
Clean:
Rm-f *. o
When you run make clean, the rm-f *. o command is executed to delete all intermediate files generated during compilation.
In any case, writing makefiles by yourself is still complicated and cumbersome, and error-prone. Therefore, GNU also provides Automake and Autoconf to help us quickly and automatically
Generate makefile. For more information, see.