此为牛客网Linux C++课程1.2&1.3的课程笔记。
0. 简介
1. gcc和g++的安装
sudo apt install gcc g++
2. gcc常用参数选项
3. gcc工作流程
首先是预处理器对源代码进行预处理(后缀名.i),主要做以下事情:
- 把头文件加入到源代码当中
- 删除源代码中的注释
- 宏替换(如define)
然后通过编译器编译成汇编代码(后缀名.s);
再通过汇编器汇编成目标代码(后缀名.o);
最后还要与启动代码、库代码、其他目标代码进行链接,最后生成可执行文件(windows为.exe,linux为.out)。
4. 代码示例
如下是名为test.c的hello world程序:
#include <stdio.h>#define PI 3.14int main() {// 测试代码int sum = PI + 10;printf("hello world");return 0;
}
可以执行
gcc test.c
这样什么参数都不加,可以直接生成a.out可执行文件,然后执行
./a.out
即输出hello world。
这一过程我们可以分步实现:
执行
gcc test.c -E -o test.i
-E 参数表示只预处理,不编译;
-o 参数表示生成test.i文件。
生成的test.i文件内容如下(中间部分省略):
# 1 "test.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "test.c"
# 1 "/usr/include/stdio.h" 1 3 4
# 27 "/usr/include/stdio.h" 3 4
# 1 "/usr/include/x86_64-linux-gnu/bits/libc-header-start.h" 1 3 4
# 33 "/usr/include/x86_64-linux-gnu/bits/libc-header-start.h" 3 4
# 1 "/usr/include/features.h" 1 3 4
# 424 "/usr/include/features.h" 3 4
# 1 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 1 3 4
# 427 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 3 4
# 1 "/usr/include/x86_64-linux-gnu/bits/wordsize.h" 1 3 4
# 428 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 2 3 4
# 1 "/usr/include/x86_64-linux-gnu/bits/long-double.h" 1 3 4
# 429 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 2 3 4
# 425 "/usr/include/features.h" 2 3 4
# 448 "/usr/include/features.h" 3 4
# 1 "/usr/include/x86_64-linux-gnu/gnu/stubs.h" 1 3 4
# 10 "/usr/include/x86_64-linux-gnu/gnu/stubs.h" 3 4
# 1 "/usr/include/x86_64-linux-gnu/gnu/stubs-64.h" 1 3 4
# 11 "/usr/include/x86_64-linux-gnu/gnu/stubs.h" 2 3 4
# 449 "/usr/include/features.h" 2 3 4
# 34 "/usr/include/x86_64-linux-gnu/bits/libc-header-start.h" 2 3 4
# 28 "/usr/include/stdio.h" 2 3 4# 1 "/usr/lib/gcc/x86_64-linux-gnu/7/include/stddef.h" 1 3 4
# 216 "/usr/lib/gcc/x86_64-linux-gnu/7/include/stddef.h" 3 4# 216 "/usr/lib/gcc/x86_64-linux-gnu/7/include/stddef.h" 3 4
typedef long unsigned int size_t;
# 34 "/usr/include/stdio.h" 2 3 4# 1 "/usr/include/x86_64-linux-gnu/bits/types.h" 1 3 4
# 27 "/usr/include/x86_64-linux-gnu/bits/types.h" 3 4
# 1 "/usr/include/x86_64-linux-gnu/bits/wordsize.h" 1 3 4
# 28 "/usr/include/x86_64-linux-gnu/bits/types.h" 2 3 4typedef unsigned char __u_char;
typedef unsigned short int __u_short;
typedef unsigned int __u_int;
typedef unsigned long int __u_long;.
.
.
.
.
.extern char *ctermid (char *__s) __attribute__ ((__nothrow__ , __leaf__));
# 840 "/usr/include/stdio.h" 3 4
extern void flockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));extern int ftrylockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));
# 868 "/usr/include/stdio.h" 3 4# 2 "test.c" 2# 5 "test.c"
int main() {int sum = 3.14 + 10;printf("hello world");return 0;
}
可见发生了头文件的替换,宏替换(PI在这里变成了3.14),以及清除了注释。
然后对test.i进行编译,执行:
gcc test.i -S -o test.s
-S参数表示编译但不汇编。
生成test.s汇编文件,内容如下:
.file "test.c".text.section .rodata
.LC0:.string "hello world".text.globl main.type main, @function
main:
.LFB0:.cfi_startprocpushq %rbp.cfi_def_cfa_offset 16.cfi_offset 6, -16movq %rsp, %rbp.cfi_def_cfa_register 6subq $16, %rspmovl $13, -4(%rbp)leaq .LC0(%rip), %rdimovl $0, %eaxcall printf@PLTmovl $0, %eaxleave.cfi_def_cfa 7, 8ret.cfi_endproc
.LFE0:.size main, .-main.ident "GCC: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0".section .note.GNU-stack,"",@progbits
可见编译了汇编代码。
最后对test.s进行汇编,生成目标代码,执行:
gcc test.s -c -o test.o
-c参数表示编译、汇编指定文件,但是不进行链接。
生成了test.o的二进制文件。
5. gcc和g++的区别