See the following example: test. c
# Include <stdio. h>
Main ()
{
Char * str = "I like Linux! I advices you jion in the Linux World ";
Printf ("% s", str );
Exit (0 );
}
Compile with gcc
Enter gcc-c test. c to obtain the target file test. o.
The-c command indicates that the file is compiled and compiled, but not connected.
If you enter gcc-o ../bin/test. o again, the executable file named test will be obtained.
In fact, these two steps cannot be completed in one breath, gcc.../bin/test. c. If the program has no errors, an executable file is generated.
Maybe you will think that the command line-based compiler is inferior to the integrated development environment such as VC. It is true that the gcc interface needs to be improved, but once you are skilled, you will feel. Gcc is so efficient. We can tell you that the powerful C/C ++ integrated development environment in Linux is as powerful as that in Vc and uses the Gcc compiler.
Gnu c compiler (gcc) is a powerful ANSI (America National standar C compatible compiler. You can operate a C compiler in other operating systems and quickly master GCC, I also learned GCC soon.
1. When using Gcc, Gcc is based on command line. It is usually followed by some options and file names. The basic usage of Gcc is as follows: the gcc [options] [filenames] command line option setting operation will execute each given file on the command line.
2. Common GCC options
Compilation options: gcc has over 100 compilation options available. For details, run the man gcc command;
Optimization Options: when compiling C/C ++ code with GCC, it will try to complete the compilation at least and the compiled code is easy to debug. Easy debugging means that the compiled code has the same execution sequence as the source code, and the compiled code has not been optimized. There are many options that tell GCC to generate smaller and faster executable files at the expense of more Compilation Time and easier debugging. The most typical options are-O and-O2. The-O option tells GCC to perform basic optimization on the source code. -The O2 option tells GCC to generate code as small and as fast as possible. Some special options can be viewed through man gcc.
Debugging and profiling options: GCC supports several debugging profiling options. Among these options, the-g and-pg.-g options tell gcc to generate debugging information that can be used by the GNU Debugger (such as gdb) to debug your program. -The pg option tells gcc to add additional code to the user's program. During execution, gprof profiling information is generated to display the time consumption of the program.
3. Use gdb
Usage: In the command line, press gdb and press enter to run gdb. After gdb is started, many options can be created on the command line, you can also run gdb in the following way: when running gdb in this way, you can directly specify the program to be debugged. Run gdb-h on the command line to obtain a simple list of gdb options.
Compile the code for debugging. To make gdb work, the program must contain debugging information during compilation. The debugging information includes the types of each variable in the program, address ing in the executable file and the source code line number. Gdb uses this information to associate the source code with the machine code.