【Linux系统编程学习】 GCC编译器

本文详细介绍了Linux下使用gcc和g++编译器的工作流程,包括预处理器处理、编译、汇编和链接的过程,并通过实例演示了Hello World程序的完整编译步骤。重点讲解了gcc和g++的区别以及它们在C++开发中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

此为牛客网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.14

int 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 4


typedef 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_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	subq	$16, %rsp
	movl	$13, -4(%rbp)
	leaq	.LC0(%rip), %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	leave
	.cfi_def_cfa 7, 8
	ret
	.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++的区别

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值