Principle and use of gcc compilation and linking

Common options for gcc and arm-linux-gcc

 

How to use gcc:

gcc [options] filename

 

gcc common options:

  -v: View the version of the gcc compiler, showing the detailed process of gcc execution

  -o    < file >             Place  the output  into   < file > 

          Specify the output file name as file, which cannot be the same as the source file name  

  -E        Preprocess only; do not compile, assemble or link

           Only preprocessing, will not compile, assemble, link

  -S        Compile only; do not assemble or link

          Only compile, will not assemble, link

  -c        Compile and assemble, but do not link

           

       

//=======================================

gcc -v: View the version of the gcc compiler

 

Way 1:

gcc hello.c outputs an  a.out , then  ./a.out to execute the application

 

gcc -o hello hello.c prints hello, then ./hello to execute the application.

 

Way 2:

gcc  -E   -o  hello.i    hello.c

gcc  -S   -o  hello.s    hello.i

gcc  -c   -o  hello.o   hello.s

gcc   -o   hello    hello.o

 

 

.o: object file (OBJ text)

summary:

1) The suffix name of the input file and the options together determine what operations gcc performs.

 

 

2) During the compilation process, unless the -E, -S, -C options are used (or the compilation error prevents the complete compilation process)

  Otherwise the last steps are all links.

 

Way 3:

gcc  -c   -o  hello.o   hello.s

gcc   -o   hello    hello.o

 

Gcc will preprocess the .c file by default, and -c will specify the compilation and assembly to get the .o file

Then link the .o file through gcc -o hello hello.o to get an executable application.

 

 Linking is to link the OBJ file generated by the assembly, the OBJ file of the system library, and the library file.

The final result is an executable program that can run on a specific platform.

 

crt1.o, crti.o, crtbegin.o, crtend.o, crtn.o are the system standard startup files added by gcc.

For general applications, these startups are required.

 

-lc: Link the libc library file, in which functions such as printf are implemented in the libc library file.

 

gcc -v -nostdlib -o hello hello.o will prompt that the link fails because the system standard startup files and standard library files are not linked.

This -nostdlib option is often used for bare metal /bootloader, linux kernel and other programs, because they do not need boot files, standard library files.

 

General applications only need system standard startup files and standard library files. ,

Bare metal/BootLoader and linux kernel lamp programs do not need startup files and standard library files.

 

Dynamic linking uses a dynamic link library for linking, and the generated program needs to load the required dynamic library when it is executed to run.

The program generated by dynamic linking is smaller in size, but must depend on the required dynamic library, otherwise it cannot be executed.

 

Static linking uses a static library for linking, and the generated program contains all the libraries required for the program to run, and can be run directly.

However, the program generated by static linking is relatively large.

 

gcc  -c  -o  hello.o  hello.c

gcc   -o  hello_shared  hello.o

gcc   -static  -o  hello_static  hello.o

 

How to use gcc:

gcc [options] filename

 

gcc common options:

  -v: View the version of the gcc compiler, showing the detailed process of gcc execution

  -o    < file >             Place  the output  into   < file > 

          Specify the output file name as file, which cannot be the same as the source file name  

  -E        Preprocess only; do not compile, assemble or link

           Only preprocessing, will not compile, assemble, link

  -S        Compile only; do not assemble or link

          Only compile, will not assemble, link

  -c        Compile and assemble, but do not link

           

       

//=======================================

gcc -v: View the version of the gcc compiler

 

Way 1:

gcc hello.c outputs an  a.out , then  ./a.out to execute the application

 

gcc -o hello hello.c prints hello, then ./hello to execute the application.

 

Way 2:

gcc  -E   -o  hello.i    hello.c

gcc  -S   -o  hello.s    hello.i

gcc  -c   -o  hello.o   hello.s

gcc   -o   hello    hello.o

 

 

.o: object file (OBJ text)

summary:

1) The suffix name of the input file and the options together determine what operations gcc performs.

 

 

2) During the compilation process, unless the -E, -S, -C options are used (or the compilation error prevents the complete compilation process)

  Otherwise the last steps are all links.

 

Way 3:

gcc  -c   -o  hello.o   hello.s

gcc   -o   hello    hello.o

 

Gcc will preprocess the .c file by default, and -c will specify the compilation and assembly to get the .o file

Then link the .o file through gcc -o hello hello.o to get an executable application.

 

 Linking is to link the OBJ file generated by the assembly, the OBJ file of the system library, and the library file.

The final result is an executable program that can run on a specific platform.

 

crt1.o, crti.o, crtbegin.o, crtend.o, crtn.o are the system standard startup files added by gcc.

For general applications, these startups are required.

 

-lc: Link the libc library file, in which functions such as printf are implemented in the libc library file.

 

gcc -v -nostdlib -o hello hello.o will prompt that the link fails because the system standard startup files and standard library files are not linked.

This -nostdlib option is often used for bare metal /bootloader, linux kernel and other programs, because they do not need boot files, standard library files.

 

General applications only need system standard startup files and standard library files. ,

Bare metal/BootLoader and linux kernel lamp programs do not need startup files and standard library files.

 

Dynamic linking uses a dynamic link library for linking, and the generated program needs to load the required dynamic library when it is executed to run.

The program generated by dynamic linking is smaller in size, but must depend on the required dynamic library, otherwise it cannot be executed.

 

Static linking uses a static library for linking, and the generated program contains all the libraries required for the program to run, and can be run directly.

However, the program generated by static linking is relatively large.

 

gcc  -c  -o  hello.o  hello.c

gcc   -o  hello_shared  hello.o

gcc   -static  -o  hello_static  hello.o

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325025885&siteId=291194637