GCC学习总结

GCC学习总结GCC GNUCompilerC GNU 编译器套件 是 GNU 项目的一部分 是一个广泛使用的开源编译器套件 支持多种编程语言和处理器架构

大家好,欢迎来到IT知识分享网。

GCC学习总结

参考教程:韦东山老师教程

1. GCC介绍

GCC(GNU Compiler Collection,GNU编译器套件)是GNU项目的一部分,是一个广泛使用的开源编译器套件,支持多种编程语言和处理器架构。GCC最初是为了编译C语言而开发的,但现在已经成为一个多语言编译器,支持多种编程语言,包括C、C++、Objective-C、Fortran、Ada、Go、D等多种语言。

GCC包含以下主要组件:

  • 前端(Frontend):负责处理源代码,将源代码转换为中间表示(Intermediate Representation,IR)。不同的编程语言有不同的前端。
  • 中间端(Middle-end):负责对中间表示进行优化,提高代码质量和性能。这是GCC的核心部分,与语言无关。
  • 后端(Backend):负责将中间表示转化为特定处理器架构的目标代码。不同的处理器架构有不同的后端。

主要特点:

  1. 跨平台:GCC支持多种操作系统(如Linux、macOS、Windows等)和处理器架构(如x86、ARM、MIPS等)。
  2. 高度可移植:由于其高度模块化的结构,GCC很容易适应新的处理器架构。
  3. 优化能力强:GCC提供了多种优化选项,可以生成高效的机器代码。
  4. 开源:作为GNU项目的一部分,GCC是完全开源的,遵循GPL许可证。

在嵌入式系统中:

  • 源文件需要经过编译才能生成可执行文件。在Windows下进行开发时,点几个按钮即可编译,因为集成开发环境(比如Visualstudio)已经将各种编译工具的使用封装好了。而Linux下更多的时候是直接使用编译工具,因此需要掌握一些编译选项。
  • PC机上的编译工具链为gcc、ld、objcopy、objdump等,它们编译出来的程序在x86平台上运行。如果要编译出能在ARM平台上运行的程序,必须使用交叉编译工具xxx-gcc、xxx-ld等(不同版本的编译器的前缀不一样,比如arm-linuxgcc)。

参考文章:交叉编译概念

2. GCC编译过程

一个C/C++文件要经过预处理(preprocessing)、编译(compilation)、汇编(assembly)和链接(linking)等4步才能变成可执行文件。

在这里插入图片描述

通过不同的GCC选项可以控制这些过程:

在这里插入图片描述

在日常交流中通常使用“编译”统称这 4 个步骤。

例如在Ubuntu环境下使用Linux的GCC编译器将hello.c文件进行编译(假如使编译的程序在IMX6ULL Pro开发板运行,就需要使用该开发板对应的编译工具链编译hello.c文件,并把编译生成的可执行程序拷贝到开发板运行):

#include <stdio.h> #define MAX 20  #define MIN 10  //#define _DEBUG #define SetBit(x) (1<<x)  int main(int argc, char* argv[]) { 
    printf("Hello World \n"); printf("MAX = %d,MIN = %d,MAX + MIN = %d\n",MAX,MIN,MAX + MIN); #ifdef _DEBUG printf("SetBit(5) = %d,SetBit(6) = %d\n",SetBit(5),SetBit(6)); printf("SetBit( SetBit(2) ) = %d\n",SetBit( SetBit(2) )); #endif  return 0; } 

2.1 预处理

C/C++源文件中,以“#”开头的命令被称为预处理命令,如包含命令“#include”、宏定义命令“#define”、条件编译命令“#if”、“#ifdef”等。预处理就是将要包含(include)的文件插入原文件中、将宏定义展开、根据条件编译命令选择要使用的代码,最后将这些东西输出到一个“.i”文件中等待进一步处理。

使用cc1命令将hello.c文件预处理生成hello.i文件:

在这里插入图片描述

在这里插入图片描述

2.2 编译

编译就是把C/C++代码(比如上述的“.i”文件)“翻译”成汇编代码,所用到的工具为cc1(它的名字就是cc1,x86有自己的cc1命令,ARM板也有自己的cc1命令)。

使用cc1命令将hello.i文件编译生成hello.s文件:

在这里插入图片描述

2.3 汇编

使用as命令将hello.s文件汇编生成hello.o文件:

在这里插入图片描述

2.4 链接

链接就是将上步生成的OBJ文件和系统库的OBJ文件、库文件链接起来,最终生成了可以在特定平台运行的可执行文件,用到的工具为ld或collect2。

使用collect2命令将hello.o文件链接生成hello可执行文件,并运行hello程序:

在这里插入图片描述

使用gcc -o hello hello.c一条命令可以完成预处理、编译、汇编和链接4个步骤,直接生成可执行文件程序:

在这里插入图片描述

3. 编译多个文件

假设当前目录下有3个文件:main.c、sub.c、sub.h:

main.c

#include <stdio.h> #include "sub.h" int main(int argc, char *argv[]) { 
    int i; printf("Main fun!\n"); sub_fun(); return 0; } 

sub.c

void sub_fun(void) { 
    printf("Sub fun!\n"); } 

sub.h

void sub_fun(void); 

编译这3个文件有2中方法:

1、一起编译:

gcc -o test main.c sub.c 

在这里插入图片描述

2、分开编译再链接到一起:

gcc -c -o main.o main.c gcc -c -o sub.o sub.c gcc -o test main.o sub.o 

在这里插入图片描述

4. 制作和使用动态库

在C语言中,动态库(Dynamic Library)是一种可执行文件或库的一部分,它在程序运行时被加载和链接。动态库提供了一个灵活的方式来组织和管理代码,使多个程序可以共享相同的代码段,从而节省内存和磁盘空间。

在Linux系统中,动态库通常以.so(Shared Object)文件的形式存在。在Windows系统中,动态库则以.dll(Dynamic Link Library)文件的形式存在。

使用动态库的好处:

  • 代码重用:动态库使得多个应用程序可以共享同一份代码。这样可以减少内存占用,并且当库中的代码需要更新时,只需要更新一次即可影响所有使用该库的应用程序。
  • 模块化开发:动态库支持模块化开发,每个功能模块可以独立开发、测试和维护,提高了开发效率和软件质量。
  • 减少磁盘空间占用:由于多个应用程序可以共享同一个动态库,因此可以减少磁盘空间的占用。
  • 易于更新:当需要更新库中的代码时,只需要替换库文件即可,无需重新编译和链接应用程序。
  • 延迟加载:动态库可以延迟加载,即在程序真正需要使用库中的函数时才加载相应的库文件,这样可以减少程序启动时的开销。

还是以main.c、sub.c、sub.h三个文件为例,制作和使用动态库。

1、制作动态库:

gcc -c -o main.o main.c gcc -c -o sub.o sub.c gcc -shared -o libsub.so sub.o sub2.o sub3.o(可以使用多个.o 生成动态库) gcc -o test main.o -lsub -L /libsub.so/所在目录/ 

在这里插入图片描述

2、使用动态库

使用动态库有2中方法:

1)把libsub.so放到Ubuntu的/lib目录中,然后就能执行test3程序了

sudo cp libsub.so /lib 

在这里插入图片描述

2)如果不想把libsub.so放到/lib,也可以放在自己指定的目录下,配置环境变量:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/a 

在这里插入图片描述

5. 制作、使用静态库

在C语言中,静态库(Static Library)是一种包含多个编译好的目标文件(.o 文件)的集合,这些目标文件在链接阶段被合并到最终的可执行文件中。

在Linux系统中,静态库通常以.a(archive)文件的形式存在。在Windows系统中,静态库则以.lib文件的形式存在。

使用静态库的好处:

  • 代码重用:静态库允许将多个源文件编译成一个库文件,这样可以在不同的项目中重复使用这些编译好的代码。
  • 模块化开发:静态库支持模块化开发,可以将程序的不同功能模块封装成独立的库文件,便于管理和维护。
  • 安全性:静态库中的代码在编译时被嵌入到可执行文件中,因此不容易被其他程序篡改或替换。
  • 独立性:静态库中的代码是独立于其他库或程序的,每个使用静态库的程序都包含了一份库中的代码副本,不会相互影响。
  • 可移植性:静态库可以与应用程序一起打包,不需要依赖外部库文件的存在,这对于部署应用程序非常方便。

静态库(Static Library)和动态库(Dynamic Library)都是用于组织和管理代码的库文件,但它们在使用方式、链接时机、更新方式等方面存在明显的区别。

静态库 动态库
链接时机 在编译阶段,静态库中的目标文件会被直接嵌入到最终生成的可执行文件中。也就是说,静态库的内容成为可执行文件的一部分。 在编译阶段,动态库中的函数只是被标记为需要在运行时加载。在程序运行时,动态库才会被加载到内存中,并与程序进行链接。
文件大小 由于静态库的内容直接嵌入到了可执行文件中,因此生成的可执行文件通常较大。 动态库的内容并不会直接嵌入到可执行文件中,而是作为一个独立的文件存在。因此,生成的可执行文件相对较小。
内存占用 静态库的内容在程序运行时全部存在于内存中,因此内存占用较大。 多个程序可以共享同一个动态库,因此内存占用相对较小。动态库的代码在内存中只需加载一次,所有使用该库的程序都可以共享这段内存。
更新 一旦静态库被链接到可执行文件中,更新静态库需要重新编译和链接整个程序才能生效。 动态库可以在不重新编译和链接应用程序的情况下更新。只要不改变动态库的接口(ABI),更新后的动态库可以直接被现有的程序使用。
依赖关系 静态库的内容被直接嵌入到可执行文件中,因此不需要额外的依赖文件。 动态库需要在运行时存在,因此需要确保动态库文件存在于系统中,并且系统知道如何找到这些文件(通过环境变量或库路径配置)。
安全性 静态库的内容在编译时被嵌入到可执行文件中,因此不容易被其他程序篡改或替换。 动态库是一个独立的文件,如果被恶意替换,可能会影响程序的正常运行。
跨平台性 静态库通常与特定的编译器和平台绑定,跨平台时需要重新编译。 动态库也与特定的平台绑定,但跨平台时只需要确保新平台上有对应的动态库版本即可。

还是以main.c、sub.c、sub.h三个文件为例,制作和使用静态库。

gcc -c -o main.o main.c gcc -c -o sub.o sub.c ar crs libsub.a sub.o sub2.o sub3.o(可以使用多个.o 生成静态库) gcc -o test main.o libsub.a (如果.a 不在当前目录下,需要指定它的绝对或相对路径) 

在这里插入图片描述

移动静态库位置后,链接时需要指定静态库的路径:

在这里插入图片描述

假如使用某交叉编译工具编译时用到静态库,编译好的程序要放到对应开发板去运行,只需要把编译好的可执行程序拷贝到开发板运行即可,不需要把静态库也拷贝过去。

6. 常用编译选项

6.1 很好用的编译选项

  • gcc -E main.c // 查看预处理结果,比如头文件是哪个 

对main.c执行的结果:

book@100ask:~/dcaStudy/02_multi_files$ gcc -E main.c # 1 "main.c" # 1 "<built-in>" # 1 "<command-line>" # 31 "<command-line>" # 1 "/usr/include/stdc-predef.h" 1 3 4 # 32 "<command-line>" 2 # 1 "main.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; typedef signed char __int8_t; typedef unsigned char __uint8_t; typedef signed short int __int16_t; typedef unsigned short int __uint16_t; typedef signed int __int32_t; typedef unsigned int __uint32_t; typedef signed long int __int64_t; typedef unsigned long int __uint64_t; typedef long int __quad_t; typedef unsigned long int __u_quad_t; typedef long int __intmax_t; typedef unsigned long int __uintmax_t; # 130 "/usr/include/x86_64-linux-gnu/bits/types.h" 3 4 # 1 "/usr/include/x86_64-linux-gnu/bits/typesizes.h" 1 3 4 # 131 "/usr/include/x86_64-linux-gnu/bits/types.h" 2 3 4 typedef unsigned long int __dev_t; typedef unsigned int __uid_t; typedef unsigned int __gid_t; typedef unsigned long int __ino_t; typedef unsigned long int __ino64_t; typedef unsigned int __mode_t; typedef unsigned long int __nlink_t; typedef long int __off_t; typedef long int __off64_t; typedef int __pid_t; typedef struct { 
    int __val[2]; } __fsid_t; typedef long int __clock_t; typedef unsigned long int __rlim_t; typedef unsigned long int __rlim64_t; typedef unsigned int __id_t; typedef long int __time_t; typedef unsigned int __useconds_t; typedef long int __suseconds_t; typedef int __daddr_t; typedef int __key_t; typedef int __clockid_t; typedef void * __timer_t; typedef long int __blksize_t; typedef long int __blkcnt_t; typedef long int __blkcnt64_t; typedef unsigned long int __fsblkcnt_t; typedef unsigned long int __fsblkcnt64_t; typedef unsigned long int __fsfilcnt_t; typedef unsigned long int __fsfilcnt64_t; typedef long int __fsword_t; typedef long int __ssize_t; typedef long int __syscall_slong_t; typedef unsigned long int __syscall_ulong_t; typedef __off64_t __loff_t; typedef char *__caddr_t; typedef long int __intptr_t; typedef unsigned int __socklen_t; typedef int __sig_atomic_t; # 36 "/usr/include/stdio.h" 2 3 4 # 1 "/usr/include/x86_64-linux-gnu/bits/types/__FILE.h" 1 3 4 struct _IO_FILE; typedef struct _IO_FILE __FILE; # 37 "/usr/include/stdio.h" 2 3 4 # 1 "/usr/include/x86_64-linux-gnu/bits/types/FILE.h" 1 3 4 struct _IO_FILE; typedef struct _IO_FILE FILE; # 38 "/usr/include/stdio.h" 2 3 4 # 1 "/usr/include/x86_64-linux-gnu/bits/libio.h" 1 3 4 # 35 "/usr/include/x86_64-linux-gnu/bits/libio.h" 3 4 # 1 "/usr/include/x86_64-linux-gnu/bits/_G_config.h" 1 3 4 # 19 "/usr/include/x86_64-linux-gnu/bits/_G_config.h" 3 4 # 1 "/usr/lib/gcc/x86_64-linux-gnu/7/include/stddef.h" 1 3 4 # 20 "/usr/include/x86_64-linux-gnu/bits/_G_config.h" 2 3 4 # 1 "/usr/include/x86_64-linux-gnu/bits/types/__mbstate_t.h" 1 3 4 # 13 "/usr/include/x86_64-linux-gnu/bits/types/__mbstate_t.h" 3 4 typedef struct { 
    int __count; union { 
    unsigned int __wch; char __wchb[4]; } __value; } __mbstate_t; # 22 "/usr/include/x86_64-linux-gnu/bits/_G_config.h" 2 3 4 typedef struct { 
    __off_t __pos; __mbstate_t __state; } _G_fpos_t; typedef struct { 
    __off64_t __pos; __mbstate_t __state; } _G_fpos64_t; # 36 "/usr/include/x86_64-linux-gnu/bits/libio.h" 2 3 4 # 53 "/usr/include/x86_64-linux-gnu/bits/libio.h" 3 4 # 1 "/usr/lib/gcc/x86_64-linux-gnu/7/include/stdarg.h" 1 3 4 # 40 "/usr/lib/gcc/x86_64-linux-gnu/7/include/stdarg.h" 3 4 typedef __builtin_va_list __gnuc_va_list; # 54 "/usr/include/x86_64-linux-gnu/bits/libio.h" 2 3 4 # 149 "/usr/include/x86_64-linux-gnu/bits/libio.h" 3 4 struct _IO_jump_t; struct _IO_FILE; typedef void _IO_lock_t; struct _IO_marker { 
    struct _IO_marker *_next; struct _IO_FILE *_sbuf; int _pos; # 177 "/usr/include/x86_64-linux-gnu/bits/libio.h" 3 4 }; enum __codecvt_result { 
    __codecvt_ok, __codecvt_partial, __codecvt_error, __codecvt_noconv }; # 245 "/usr/include/x86_64-linux-gnu/bits/libio.h" 3 4 struct _IO_FILE { 
    int _flags; char* _IO_read_ptr; char* _IO_read_end; char* _IO_read_base; char* _IO_write_base; char* _IO_write_ptr; char* _IO_write_end; char* _IO_buf_base; char* _IO_buf_end; char *_IO_save_base; char *_IO_backup_base; char *_IO_save_end; struct _IO_marker *_markers; struct _IO_FILE *_chain; int _fileno; int _flags2; __off_t _old_offset; unsigned short _cur_column; signed char _vtable_offset; char _shortbuf[1]; _IO_lock_t *_lock; # 293 "/usr/include/x86_64-linux-gnu/bits/libio.h" 3 4 __off64_t _offset; void *__pad1; void *__pad2; void *__pad3; void *__pad4; size_t __pad5; int _mode; char _unused2[15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t)]; }; typedef struct _IO_FILE _IO_FILE; struct _IO_FILE_plus; extern struct _IO_FILE_plus _IO_2_1_stdin_; extern struct _IO_FILE_plus _IO_2_1_stdout_; extern struct _IO_FILE_plus _IO_2_1_stderr_; # 337 "/usr/include/x86_64-linux-gnu/bits/libio.h" 3 4 typedef __ssize_t __io_read_fn (void *__cookie, char *__buf, size_t __nbytes); typedef __ssize_t __io_write_fn (void *__cookie, const char *__buf, size_t __n); typedef int __io_seek_fn (void *__cookie, __off64_t *__pos, int __w); typedef int __io_close_fn (void *__cookie); # 389 "/usr/include/x86_64-linux-gnu/bits/libio.h" 3 4 extern int __underflow (_IO_FILE *); extern int __uflow (_IO_FILE *); extern int __overflow (_IO_FILE *, int); # 433 "/usr/include/x86_64-linux-gnu/bits/libio.h" 3 4 extern int _IO_getc (_IO_FILE *__fp); extern int _IO_putc (int __c, _IO_FILE *__fp); extern int _IO_feof (_IO_FILE *__fp) __attribute__ ((__nothrow__ , __leaf__)); extern int _IO_ferror (_IO_FILE *__fp) __attribute__ ((__nothrow__ , __leaf__)); extern int _IO_peekc_locked (_IO_FILE *__fp); extern void _IO_flockfile (_IO_FILE *) __attribute__ ((__nothrow__ , __leaf__)); extern void _IO_funlockfile (_IO_FILE *) __attribute__ ((__nothrow__ , __leaf__)); extern int _IO_ftrylockfile (_IO_FILE *) __attribute__ ((__nothrow__ , __leaf__)); # 462 "/usr/include/x86_64-linux-gnu/bits/libio.h" 3 4 extern int _IO_vfscanf (_IO_FILE * __restrict, const char * __restrict, __gnuc_va_list, int *__restrict); extern int _IO_vfprintf (_IO_FILE *__restrict, const char *__restrict, __gnuc_va_list); extern __ssize_t _IO_padn (_IO_FILE *, int, __ssize_t); extern size_t _IO_sgetn (_IO_FILE *, void *, size_t); extern __off64_t _IO_seekoff (_IO_FILE *, __off64_t, int, int); extern __off64_t _IO_seekpos (_IO_FILE *, __off64_t, int); extern void _IO_free_backup_area (_IO_FILE *) __attribute__ ((__nothrow__ , __leaf__)); # 42 "/usr/include/stdio.h" 2 3 4 typedef __gnuc_va_list va_list; # 57 "/usr/include/stdio.h" 3 4 typedef __off_t off_t; # 71 "/usr/include/stdio.h" 3 4 typedef __ssize_t ssize_t; typedef _G_fpos_t fpos_t; # 131 "/usr/include/stdio.h" 3 4 # 1 "/usr/include/x86_64-linux-gnu/bits/stdio_lim.h" 1 3 4 # 132 "/usr/include/stdio.h" 2 3 4 extern struct _IO_FILE *stdin; extern struct _IO_FILE *stdout; extern struct _IO_FILE *stderr; extern int remove (const char *__filename) __attribute__ ((__nothrow__ , __leaf__)); extern int rename (const char *__old, const char *__new) __attribute__ ((__nothrow__ , __leaf__)); extern int renameat (int __oldfd, const char *__old, int __newfd, const char *__new) __attribute__ ((__nothrow__ , __leaf__)); extern FILE *tmpfile (void) ; # 173 "/usr/include/stdio.h" 3 4 extern char *tmpnam (char *__s) __attribute__ ((__nothrow__ , __leaf__)) ; extern char *tmpnam_r (char *__s) __attribute__ ((__nothrow__ , __leaf__)) ; # 190 "/usr/include/stdio.h" 3 4 extern char *tempnam (const char *__dir, const char *__pfx) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__malloc__)) ; extern int fclose (FILE *__stream); extern int fflush (FILE *__stream); # 213 "/usr/include/stdio.h" 3 4 extern int fflush_unlocked (FILE *__stream); # 232 "/usr/include/stdio.h" 3 4 extern FILE *fopen (const char *__restrict __filename, const char *__restrict __modes) ; extern FILE *freopen (const char *__restrict __filename, const char *__restrict __modes, FILE *__restrict __stream) ; # 265 "/usr/include/stdio.h" 3 4 extern FILE *fdopen (int __fd, const char *__modes) __attribute__ ((__nothrow__ , __leaf__)) ; # 278 "/usr/include/stdio.h" 3 4 extern FILE *fmemopen (void *__s, size_t __len, const char *__modes) __attribute__ ((__nothrow__ , __leaf__)) ; extern FILE *open_memstream (char __bufloc, size_t *__sizeloc) __attribute__ ((__nothrow__ , __leaf__)) ; extern void setbuf (FILE *__restrict __stream, char *__restrict __buf) __attribute__ ((__nothrow__ , __leaf__)); extern int setvbuf (FILE *__restrict __stream, char *__restrict __buf, int __modes, size_t __n) __attribute__ ((__nothrow__ , __leaf__)); extern void setbuffer (FILE *__restrict __stream, char *__restrict __buf, size_t __size) __attribute__ ((__nothrow__ , __leaf__)); extern void setlinebuf (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)); extern int fprintf (FILE *__restrict __stream, const char *__restrict __format, ...); extern int printf (const char *__restrict __format, ...); extern int sprintf (char *__restrict __s, const char *__restrict __format, ...) __attribute__ ((__nothrow__)); extern int vfprintf (FILE *__restrict __s, const char *__restrict __format, __gnuc_va_list __arg); extern int vprintf (const char *__restrict __format, __gnuc_va_list __arg); extern int vsprintf (char *__restrict __s, const char *__restrict __format, __gnuc_va_list __arg) __attribute__ ((__nothrow__)); extern int snprintf (char *__restrict __s, size_t __maxlen, const char *__restrict __format, ...) __attribute__ ((__nothrow__)) __attribute__ ((__format__ (__printf__, 3, 4))); extern int vsnprintf (char *__restrict __s, size_t __maxlen, const char *__restrict __format, __gnuc_va_list __arg) __attribute__ ((__nothrow__)) __attribute__ ((__format__ (__printf__, 3, 0))); # 365 "/usr/include/stdio.h" 3 4 extern int vdprintf (int __fd, const char *__restrict __fmt, __gnuc_va_list __arg) __attribute__ ((__format__ (__printf__, 2, 0))); extern int dprintf (int __fd, const char *__restrict __fmt, ...) __attribute__ ((__format__ (__printf__, 2, 3))); extern int fscanf (FILE *__restrict __stream, const char *__restrict __format, ...) ; extern int scanf (const char *__restrict __format, ...) ; extern int sscanf (const char *__restrict __s, const char *__restrict __format, ...) __attribute__ ((__nothrow__ , __leaf__)); # 395 "/usr/include/stdio.h" 3 4 extern int fscanf (FILE *__restrict __stream, const char *__restrict __format, ...) __asm__ ("" "__isoc99_fscanf") ; extern int scanf (const char *__restrict __format, ...) __asm__ ("" "__isoc99_scanf") ; extern int sscanf (const char *__restrict __s, const char *__restrict __format, ...) __asm__ ("" "__isoc99_sscanf") __attribute__ ((__nothrow__ , __leaf__)) ; # 420 "/usr/include/stdio.h" 3 4 extern int vfscanf (FILE *__restrict __s, const char *__restrict __format, __gnuc_va_list __arg) __attribute__ ((__format__ (__scanf__, 2, 0))) ; extern int vscanf (const char *__restrict __format, __gnuc_va_list __arg) __attribute__ ((__format__ (__scanf__, 1, 0))) ; extern int vsscanf (const char *__restrict __s, const char *__restrict __format, __gnuc_va_list __arg) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__format__ (__scanf__, 2, 0))); # 443 "/usr/include/stdio.h" 3 4 extern int vfscanf (FILE *__restrict __s, const char *__restrict __format, __gnuc_va_list __arg) __asm__ ("" "__isoc99_vfscanf") __attribute__ ((__format__ (__scanf__, 2, 0))) ; extern int vscanf (const char *__restrict __format, __gnuc_va_list __arg) __asm__ ("" "__isoc99_vscanf") __attribute__ ((__format__ (__scanf__, 1, 0))) ; extern int vsscanf (const char *__restrict __s, const char *__restrict __format, __gnuc_va_list __arg) __asm__ ("" "__isoc99_vsscanf") __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__format__ (__scanf__, 2, 0))); # 477 "/usr/include/stdio.h" 3 4 extern int fgetc (FILE *__stream); extern int getc (FILE *__stream); extern int getchar (void); # 495 "/usr/include/stdio.h" 3 4 extern int getc_unlocked (FILE *__stream); extern int getchar_unlocked (void); # 506 "/usr/include/stdio.h" 3 4 extern int fgetc_unlocked (FILE *__stream); # 517 "/usr/include/stdio.h" 3 4 extern int fputc (int __c, FILE *__stream); extern int putc (int __c, FILE *__stream); extern int putchar (int __c); # 537 "/usr/include/stdio.h" 3 4 extern int fputc_unlocked (int __c, FILE *__stream); extern int putc_unlocked (int __c, FILE *__stream); extern int putchar_unlocked (int __c); extern int getw (FILE *__stream); extern int putw (int __w, FILE *__stream); extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream) ; # 603 "/usr/include/stdio.h" 3 4 extern __ssize_t __getdelim (char __restrict __lineptr, size_t *__restrict __n, int __delimiter, FILE *__restrict __stream) ; extern __ssize_t getdelim (char __restrict __lineptr, size_t *__restrict __n, int __delimiter, FILE *__restrict __stream) ; extern __ssize_t getline (char __restrict __lineptr, size_t *__restrict __n, FILE *__restrict __stream) ; extern int fputs (const char *__restrict __s, FILE *__restrict __stream); extern int puts (const char *__s); extern int ungetc (int __c, FILE *__stream); extern size_t fread (void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __stream) ; extern size_t fwrite (const void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __s); # 673 "/usr/include/stdio.h" 3 4 extern size_t fread_unlocked (void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __stream) ; extern size_t fwrite_unlocked (const void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __stream); extern int fseek (FILE *__stream, long int __off, int __whence); extern long int ftell (FILE *__stream) ; extern void rewind (FILE *__stream); # 707 "/usr/include/stdio.h" 3 4 extern int fseeko (FILE *__stream, __off_t __off, int __whence); extern __off_t ftello (FILE *__stream) ; # 731 "/usr/include/stdio.h" 3 4 extern int fgetpos (FILE *__restrict __stream, fpos_t *__restrict __pos); extern int fsetpos (FILE *__stream, const fpos_t *__pos); # 757 "/usr/include/stdio.h" 3 4 extern void clearerr (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)); extern int feof (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; extern int ferror (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; extern void clearerr_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)); extern int feof_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; extern int ferror_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; extern void perror (const char *__s); # 1 "/usr/include/x86_64-linux-gnu/bits/sys_errlist.h" 1 3 4 # 26 "/usr/include/x86_64-linux-gnu/bits/sys_errlist.h" 3 4 extern int sys_nerr; extern const char *const sys_errlist[]; # 782 "/usr/include/stdio.h" 2 3 4 extern int fileno (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; extern int fileno_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; # 800 "/usr/include/stdio.h" 3 4 extern FILE *popen (const char *__command, const char *__modes) ; extern int pclose (FILE *__stream); 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 "main.c" 2 # 1 "sub.h" 1 # 1 "sub.h" void sub_fun(void); # 3 "main.c" 2 int main(int argc, char *argv[]) { 
    int i; printf("Main fun!\n"); sub_fun(); return 0; } book@100ask:~/dcaStudy/02_multi_files$ 
  • gcc -E -dM main.c > 1.txt // 把所有的宏展开,存在 1.txt 里 

对main文件使用结果:

在这里插入图片描述

  • gcc -Wp,-MD,abc.dep -c -o main.o main.c // 生成依赖文件 abc.dep,一般Makefile会用到 

在这里插入图片描述

  • echo 'main(){}'| gcc -E -v - // 它会列出头文件目录、库目录(LIBRARY_PATH) 

在这里插入图片描述

6.2 GCC总体选项(OverallOption)

1、-c
预处理、编译和汇编源文件,但是不作链接,编译器根据源文件生成OBJ文件。缺省情况下,GCC通过用.o替换源文件名的后缀.c.i.s等,产生OBJ文件名。可以使用-o选项选择其他名字。GCC忽略-c选项后面任何无法识别的输入文件。

2、-S
编译后即停止,不进行汇编。对于每个输入的非汇编语言文件,输出结果是汇编语言文件。缺省情况下,GCC通过用.s替换源文件名后缀.c.i等等,产生汇编文件名。可以使用-o选项选择其他名字。GCC忽略任何不需要汇编的输入文件。

3、-E
预处理后即停止,不进行编译。预处理后的代码送往标准输出。

4、-o file
指定输出文件为file。无论是预处理、编译、汇编还是链接,这个选项都可以使用。如果没有使用-o选项,默认的输出结果是:可执行文件为a.out;修改输入文件的名称是source.suffix,则它的OBJ文件是source.o,汇编文件是source.s,而预处理后的C源代码送往标准输出。

5、-v
显示制作GCC工具自身时的配置命令;同时显示编译器驱动程序、预处理器、编译器的版本号。

6.3 警告选项(Warning Option)-Wall

这个选项基本打开了所有需要注意的警告信息,比如没有指定类型的声明、在声明之前就使用的函数、局部变量除了声明就没再使用等。

在这里插入图片描述

6.4 调试选项(Debugging Option)-g

以操作系统的本地格式(stabs,COFF,XCOFF,或DWARF)产生调试信息,GDB能够使用这些调试信息。在大多数使用stabs格式的系统上,-g选项加入只有GDB才使用的额外调试信息。可以使用下面的选项来生成额外的信息:-gstabs+-gstabs-gxcoff+-gxcoff-gdwarf+-gdwarf,具体用法请读者参考GCC手册。

6.5 优化选项(Optimization Option)

1、-O或者-O1

优化:对于大函数,优化编译的过程将占用稍微多的时间和相当大的内存。不使用-O'或-O1’选项的目的是减少编译的开销,使编译结果能够调试、语句是独立的:如果在两条语句之间用断点中止程序,可以对任何变量重新赋值,或者在函数体内把程序计数器指到其他语句,以及从源程序中精确地获取你所期待的结果。

不使用-O'或-O1’选项时,只有声明了register的变量才分配使用寄存器

使用了-O'或-O1’选项,编译器会试图减少目标码的大小和执行时间。如果指定了-O-O1选项,,-fthread-jumps-fdefer-pop选项将被打开。在有delayslot的机器上,-fdelayed-branch选项将被打开。在即使没有帧指针(frame pointer)也支持调试的机器上,-fomit-framepointer选项将被打开。某些机器上还可能会打开其他选项。

2、-O2

多优化一些。除了涉及空间和速度交换的优化选项,执行几乎所有的优化工作。例如不进行循环展开(loop unrolling)和函数内嵌(inlining)。和-O-O1选项比较,这个选项既增加了编译时间,也提高了生成代码的运行效果。

3、-O3

优化的更多。除了打开-O2所做的一切,它还打开了-finline-functions选项。

4、-O0

在一般应用中,经常使用-O2选项。

6.6 链接器选项(Linker Option)

1、object-file-name

2、-llibrary

链接名为library的库文件。链接器在标准搜索目录中寻找这个库文件,库文件的真正名字是liblibrary.a。搜索目录除了一些系统标准目录外,还包括用户以-L选项指定的路径。一般说来用这个方法找到的文件是库文件──即由OBJ文件组成的归档文件(archive file)。链接器处理归档文件的方法是:扫描归档文件,寻找某些成员,这些成员的符号目前已被引用,不过还没有被定义。但是,如果链接器找到普通的OBJ文件,而不是库文件,就把这个OBJ文件按平常方式链接进来。指定-l选项和指定文件名的唯一区别是,-l选项用lib.a把library包裹起来,而且搜索一些目录。
即使不明显地使用-llibrary选项,一些默认的库也被链接进去,可以使用-v选项看到这点:

在这里插入图片描述

3、-nostartfiles

不链接系统标准启动文件,而标准库文件仍然正常使用:

在这里插入图片描述

启动文件crt1.o、crti.o、crtend.o、crtn.o没有被链接进去。需要说明的是,对于一般应用程序,这些启动文件是必需的,这里仅是作为例子(这样编译出来的test文件无法执行)。在编译bootloader、内核时,将用到这个选项。

4、-nostdlib

不链接系统标准启动文件和标准库文件,只把指定的文件传递给链接器。这个选项常用于编译内核、bootloader等程序,它们不需要启动文件、标准库文件。

在这里插入图片描述

出现了一大堆错误,因为printf等函数是在库文件中实现的。在编译bootloader、内核时,用到这个选项──它们用到的很多函数是自包含的。

5、-static

在这里插入图片描述

其中test6文件为8360字节,test7_static文件为字节。当不使用-static编译文件时,程序执行前要链接共享库文件,所以还需要将共享库文件放入文件系统中。

6、-shared

gcc -c -o sub.o sub.c gcc -shared -o libsub.so sub.o 

以后要使用sub.c中的函数sub_fun时,在链接程序时,指定引脚libsub.so即可,比如:

gcc -o test main.o -lsub -L /libsub.so/所在的目录/ 

可以将多个文件制作为一个库文件,比如:

gcc -shared -o libsub.so sub.o sub2.o sub3.o 

7、-Xlinker option

把选项option传递给链接器。可以用来传递系统特定的链接选项,GCC无法识别这些选项。如果需要传递携带参数的选项,必须使用两次-Xlinker,一次传递选项,另一次传递其参数。例如,如果传递-assert definitions,要成-Xlinker -assert -Xlinker definitions,而不能写成-Xlinker "-assert definitions",因为这样会把整个字符串当做一个参数传递,显然这不是链接器期待的。

8、-Wl,option

把选项option传递给链接器。如果option中含有逗号,就在逗号处分割成多个选项。链接器通常是通过gcc、arm-linux-gcc等命令间接启动的,要向它传入参数时,参数前面加上-Wl,

9、-u symbol

使链接器认为取消了symbol的符号定义,从而链接库模块以取得定义。可以使用多个-u选项,各自跟上不同的符号,使得链接器调入附加的库模块。

6.7 目录选项(Directory Option)

1、-Idir

2、-I-

任何在-I-前面用-I选项指定的搜索路径只适用于#include"file"这种情况;它们不能用来搜索#include<file>包含的头文件。如果用-I选项指定的搜索路径位于-I-选项后面,就可以在这些路径中搜索所有的#include指令(一般说来-I选项就是这么用的)。还有,-I-选项能够阻止当前目录(存放当前输入文件的地方)成为搜索#include"file"的第一选择。
-I-不影响使用系统标准目录,因此,-I--nostdinc是不同的选项。

3、-Ldir

-l选项的搜索路径列表中添加dir目录。仍使用options程序进行说明,先制作库文件 libsub. a:

在这里插入图片描述

4、-Bprefix

这个选项指出在何处寻找可执行文件,库文件,以及编译器自己的数据文件。编译器驱动程序需要使用某些工具,比如:cppcc1(或C++的cc1plus),asld。它把prefix当作欲执行的工具的前缀,这个前缀可以用来指定目录,也可以用来修改工具名字。

对于要运行的工具,编译器驱动程序首先试着加上-B前缀(如果存在),如果没有找到文件,或没有指定-B选项,编译器接着会试验两个标准前缀/usr/lib/gcc//usr/local/lib/gcc-lib/。如果仍然没能够找到所需文件,编译器就在PATH环境变量指定的路径中寻找没加任何前缀的文件名。如果有需要,运行时(run-time)支持文件libgcc.a也在-B前缀的搜索范围之内。如果这里没有找到,就在上面提到的两个标准前缀中寻找,仅此而已。如果上述方法没有找到这个文件,就不链接它了。多数情况的多数机器上,libgcc.a并非必不可少。
可以通过环境变量GCC_EXEC_PREFIX获得近似的效果;如果定义了这个变量,其值就和上面说的一样被用作前缀。如果同时指定了-B选项和GCC_EXEC_PREFIX变量,编译器首先使用-B选项,然后才尝试环境变量值。

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/121473.html

(0)
上一篇 2025-10-23 14:15
下一篇 2025-10-23 14:20

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信