Detailed explanation of the use of Linux compiler gcc/g++

Linux compiler-gcc/g++ use

  • Compiler: gcc compiles C language, g++ compiles C language/C++;
  • Gcc complete format: gcc [option] [file to be compiled] [option] [object file]
  • The compilation process of the program: preprocessing (for macro replacement), compilation (generating assembly), assembly (generating machine identifiable code), linking (generating executable files or library files)

Pretreatment:

      1. 预处理功能主要包括宏定义,文件包含,文件编译,去注释等。
      2. 预处理指令是以 # 号开头的代码行。
      3. 实例:gcc  -E  hello.c  -o  hello.i
      4. 选项  " -E " ,该选项的作用是让gcc在预处理结束后停止编译过程。
      5. 选项 " -o " 是指目标文件," i "文件为已经预处理的C源程序

Compile:

      1. 在这个阶段中,gcc首先要检查代码的规范性、是否有语法错误等,以确定代码实际要做的工作,
          在检查无误后,gcc把代码翻译成汇编语言。 
      2. 用户可以使用 “ -S-”选项进行查看,该选项进行进行编译而不进行汇编,生成汇编代码。
      3. 实例:gcc   -s  hello.i  -o  hello.s

compilation:

     1. 汇编阶段是把编译阶段生成的 “ .s ”文件转成目标文件
     2. 读者在此可使用选项 " -c " 就可以看到汇编代码已转化为 " .o "的二进制目标代码了,
     “ .o ”文件对标到win当中,就是目标程序(xxx.obj)
     4. 实例:gcc  -c  hello.s  -o  hello.o

link:

     1.在成功编译之后,就进入了链接阶段,将多个目标和库链接到一起,生成可执行程序
     2. 实例:gcc  hello.o  -o  hello
  • An important concept is involved here: We have not defined the function implementation of "printf" in the library function
    in the C program, and there is only the declaration of the function in the "stdio.h" included in the precompilation, but no function definition. So, where is the "printf" function implemented?
    In fact, the implementation of these functions has been put by the system into a library file named libc.so.6. Unless otherwise specified, gcc will search under the system default search path "/usr/lib". It is to link to the libc.so.6 library function, so that the function "printf" can be realized, and this is the function of the link

  • The function library is generally divided into two types : static library and dynamic library
    . 1. Static library means that when compiling and linking, all the code of the library file is added to the executable file, so the generated file is relatively large, but the library file is not needed at runtime Up. The suffix name is generally ".a"
    2. In contrast to the dynamic library, the code of the library file is not added to the executable file when compiling and linking, but the library is loaded by the runtime link file when the program is executed. Can save system overhead. The general suffix of the dynamic library is ".so", if the aforementioned libc.so.6 is a dynamic library. gcc uses dynamic libraries by default when compiling. After the link is completed, gcc can generate an executable file, as shown below: gcc hello.o -o hello
    3. The binary program generated by gcc by default is dynamically linked. This can be verified by the file command.

  • gcc options

     1. -E 中激活预处理,这个不生成文件,你需要把它重定向到一个输出文件里面
     2. -S 编译到汇编语言不进行汇编和链接
     3. -c编译到目标代码
     4. -o文件输出到文件
     5. -static此选项对生成的文件采用静态链接
     6. -g生成调试信息。GNU调试器可利用该信息
     7. -shared此选项将尽量使用动态库,所以生成文件比较小,但是需要系统由动态库。
     8. -o0
     9. -o1
     10. -o2
     11. -o3 编译器的优化选项的4个级别,-o0表示没有优化,-o1为缺省值,-o3优化级别最高
     12. -w 不生成任何警告信息
     13. -Wall生成所有警告信息
    

Guess you like

Origin blog.csdn.net/weixin_45796387/article/details/114676314