GCC compiler primer [reproduced], gcc compiler primer reprint
GCC Compiler (GNU C Compiler) is an open source compiler from the GNU organization. It is the default C compiler in the Linux environment. It deals with the ability to efficiently compile C and other languages. And, GCC now includes not only the compiler itself, but also the toolchain during the compilation process.
1 GCC compilation process
Before learning to use GCC to compile programs, you must first understand the basic process of compiling C programs. Generally, it is divided into the following four steps:
(1) Preprocess the C language and generate * .i files.
(2) Compile the * .i file generated in the previous step to generate an assembly language file with the suffix * .s
(3) Assemble the assembly language file * .s to generate an object file with the suffix * .o
(4) Link the * .o files of each module to generate the final executable file
2 GCC common options
GCC has a lot of compilation options, there are thousands of them now, but we don't use many of them. Below we only introduce some of them which are very practical.
Before that, let's write the following source files in preparation for testing.
1 //main.c
2 #include <stdio.h>
3
4 extern int add (int a, int b);
5 extern int mul (int a, int b);
6
7 int main (void)
8 {
9 int a = 10, b = 5;
10 int result;
11
12 result = add (a, mul (a, b));
13 printf ("result =% d \ n", result);
14 return 0;
15}
1 //test1.c
2 int add (int a, int b)
3 {
4 return a + b;
5}
1 //test2.c
2 int mul (int a, int b)
3 {
4 return a * b;
5}
2.1 -E option
This option preprocesses the C language source file, but does not compile the program. For general preprocessing problems (such as macro expansion problems, file inclusion problems, etc.), you can use this option to view. In addition, if you use this option directly, the program will directly output the preprocessed results to the terminal, which is not easy to view. Therefore, you can generally use redirection to save the program output results to a text file in the following format:
gcc -E source.c> output
If we precompile main.c, the output file content is as follows:
1 # 1 "main.c"
2 # 1 "<command-line>"
3 # 1 "/usr/include/stdc-predef.h" 1 3 4
4 # 1 "<command-line>" 2
5 # 1 "main.c"
6
7 # 1 "/usr/include/stdio.h" 1 3 4
8 # 27 "/usr/include/stdio.h" 3 4
9 # 1 "/usr/include/features.h" 1 3 4
10 # 374 "/usr/include/features.h" 3 4
11 # 1 "/usr/include/i386-linux-gnu/sys/cdefs.h" 1 3 4
12 # 385 "/usr/include/i386-linux-gnu/sys/cdefs.h" 3 4
13 # 1 "/usr/include/i386-linux-gnu/bits/wordsize.h" 1 3 4
14 # 386 "/usr/include/i386-linux-gnu/sys/cdefs.h" 2 3 4
15 # 375 "/usr/include/features.h" 2 3 4
16 # 398 "/usr/include/features.h" 3 4
17 # 1 "/usr/include/i386-linux-gnu/gnu/stubs.h" 1 3 4
18
19
20
twenty one
twenty two
twenty three
...............
826
827 extern int pclose (FILE * __ stream);
828
829
830
831
832
833 extern char * ctermid (char * __ s) __attribute__ ((__nothrow__, __leaf__));
834 # 913 "/usr/include/stdio.h" 3 4
835 extern void flockfile (FILE * __ stream) __attribute__ ((__nothrow__, __leaf__));
836
837
838
839 extern int ftrylockfile (FILE * __ stream) __attribute__ ((__nothrow__, __leaf__));
840
841
842 extern void funlockfile (FILE * __ stream) __attribute__ ((__nothrow__, __leaf__));
843 # 943 "/usr/include/stdio.h" 3 4
844
845 # 3 "main.c" 2
846
847 extern int add (int a, int b);
848 extern int mul (int a, int b);
849
850 int main (void)
851 {
852 int a = 10, b = 5;
853 int result;
854
855 result = add (a, mul (a, b));
856 printf ("result =% d \ n", result);
857 return 0;
858}
Of course, we can also use the -o command to introduce the output file name, the format is as follows:
gcc -E source.c -o source.i
Use this command to preprocess main.c. The content of the main.i file obtained is exactly the same as the output file obtained from the redirected output.
2.2 -S option
This option (uppercase S) compiles C language source files into assembly language files, but does not assemble the program. Note: The role of the assembly process is to compile the assembly language file into an object file * .o, while the -S option is used to obtain the assembly language file * .s. The use of this option is:
gcc -S source.c
With this option, an assembly language file with the same name as the source file, but ending in * .s, is finally generated.
xiaomanon @ xiaomanon-machine: ~ / Documents / c_code $ gcc -S test1.c
xiaomanon @ xiaomanon-machine: ~ / Documents / c_code $ ls
main.c test1.c test1.s test2.c
Of course, there is more than one input source file. You can compile all C language source files in the current directory:
xiaomanon @ xiaomanon-machine: ~ / Documents / c_code $ gcc -S * .c
xiaomanon @ xiaomanon-machine: ~ / Documents / c_code $ ls
main.c main.s test1.c test1.s test2.c test2.s
We can also look at the generated assembly language code:
选项 This option (lowercase c) means to compile and assemble the specified source file, but not to link. This option is used as follows:
gcc -c source.c
That is, the C source file to be compiled and assembled is immediately followed by the -c option, and an object file with the same name as the source file but with the suffix ending in * .o is generated.
xiaomanon @ xiaomanon-machine: ~ / Documents / c_code $ ls
main.c test1.c test2.c
xiaomanon @ xiaomanon-machine: ~ / Documents / c_code $ gcc -c test1.c
xiaomanon @ xiaomanon-machine: ~ / Documents / c_code $ ls
main.c test1.c test1.o test2.c
It can be seen that the corresponding * .o object file is generated after compiling with the -c option. Of course, you can also specify multiple C source files at once. After using the -c option, a corresponding * .o object file will be generated for each C source file.
This option (lowercase O) is used to compile the input file and output a file with the specified name. There are two ways to use this option. The first is immediately after the gcc command:
gcc -o app source1.c source2.c source3.c
Then, we can compile our test program in the following way:
xiaomanon @ xiaomanon-machine: ~ / Documents / c_code $ gcc -o app * .c
xiaomanon @ xiaomanon-machine: ~ / Documents / c_code $ ls
app main.c test1.c test2.c
xiaomanon @ xiaomanon-machine: ~ / Documents / c_code $ ./app
result = 60
There is another way, the -o option is placed last:
gcc source1.c source2.c source3.c -o app
This method is more logical, and the semantics can be understood as compiling the C language source file to get the final executable program app. Using this method to compile our test program is as follows:
In addition, this option is often used when linking multiple object files. We may need to perform corresponding operations on different source files to obtain the object files * .o, and then link these object files into an executable file at the end. .
2.5 -I option
This option is used to specify the directory of included header files, which is useful for large code organizations.
2.6 -g option
This option is used to generate debugging information that can be used by the gdb debugger. Only the executable file generated after using this option will have the symbol table referenced in the program, which is the gdb debugger can debug the executable program.
xiaomanon @ xiaomanon-machine: ~ / Documents / c_code $ gcc * .c -o app
xiaomanon @ xiaomanon-machine: ~ / Documents / c_code $ ll app
-rwxrwxr-x 1 xiaomanon xiaomanon 7381 Dec 29 15:23 app *
xiaomanon @ xiaomanon-machine: ~ / Documents / c_code $ gcc -g * .c -o app
xiaomanon @ xiaomanon-machine: ~ / Documents / c_code $ ll app
-rwxrwxr-x 1 xiaomanon xiaomanon 8825 Dec 29 15:23 app *
The above command generates an executable file without debugging information and an executable file with debugging information, and compares the file sizes of the two. It can be seen that the executable file generated using the -g option is significantly more than the executable file without using The -g option has a large executable file.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.