C预处理器详解:GCC 11.1.0版及其工作原理

需积分: 9 0 下载量 114 浏览量 更新于2024-07-09 收藏 466KB PDF 举报
C预处理器是C语言编译过程中的一个关键组件,它在源代码编译之前对文本进行处理,以执行宏定义、条件编译、包含头文件等功能。本文档主要针对GCC版本11.1.0,由Richard Stallman和Zachary Weinberg撰写,版权归属Free Software Foundation。文档遵循GNU Free Documentation License Version 1.3或更高版本,强调了复制、分发和修改的自由。 1. **概述** - **字符集**: C预处理器支持多种字符集,包括ASCII、ISO Latin-1等,以处理不同编码的源代码。 - **初始处理**: 预处理器首先处理输入,识别并转换行结束符(如Unix的换行符或Windows的CR+LF),确保跨平台兼容性。 - **词法分析(Tokenization)**: 将源代码分解成可识别的基本单元(tokens),如标识符、关键字、运算符等。 2. **头文件管理** - **包含语法**: #include指令用于引入其他源文件,提供所需的功能和声明。 - **包含操作**: 预处理器查找指定路径中的头文件,并将其中的内容插入到源代码中。 - **搜索路径**: 编译器有预定义的搜索路径,用于寻找包含文件,也可以通过命令行选项自定义。 - **一次仅包含(Once-Only Headers)**: 防止重复包含同一个头文件的机制,如#ifndef/define/endif结构。 - **替代方案**: 提供了其他方式来避免头文件重复,比如条件编译的替代方法。 - **计算包含(Computed Includes)**: 使用宏进行动态头文件路径的计算,增加代码的灵活性。 - **包装头文件(Wrapper Headers)**: 一种策略,用包含检查来控制代码的可见性,防止无意的循环依赖。 C预处理器不仅仅是简单的文本替换,它的作用远超乎普通文本编辑器的功能,它能根据编译时的环境变量和条件语句改变代码的行为。理解并正确使用预处理器是C程序员必备的技能,因为它能帮助编写更模块化、灵活且高效的代码。掌握预处理器的细节对于编写高性能、可维护的程序至关重要。

checking whether the compiler supports GNU C++... yes checking whether g++ accepts -g... yes checking for g++ option to enable C++11 features... none needed checking dependency style of g++... gcc3 checking how to run the C preprocessor... gcc -std=gnu11 -E checking for x86_64-w64-mingw32-ranlib... no checking for ranlib... ranlib checking for x86_64-w64-mingw32-dlltool... no checking for dlltool... no checking for x86_64-w64-mingw32-ar... no checking for x86_64-w64-mingw32-lib... no checking for x86_64-w64-mingw32-link... no checking for ar... ar checking the archiver (ar) interface... ar checking dependency style of gcc -std=gnu11... gcc3 checking for x86_64-w64-mingw32-as... no checking for as... as checking whether dlltool supports --temp-prefix... yes checking whether to build a w32api package for Cygwin... no checking whether to build the Win32 libraries... yes checking whether to build the Win64 libraries... yes checking whether to build the WinARM32 libraries... no checking whether to build the WinARM64 libraries... no checking whether to use genlib... no checking whether to enable globbing... no checking whether to enable private exports... no checking whether to enable delay import libs... no checking what to provide as libmsvcrt.a... msvcrt-os checking whether to include support for Control Flow Guard... no checking whether to enable experimental features... no checking whether the compiler supports -municode... no checking for stdio.h... yes checking for stdlib.h... yes checking for string.h... yes checking for inttypes.h... yes checking for stdint.h... yes checking for strings.h... yes checking for sys/stat.h... yes checking for sys/types.h... yes checking for unistd.h... yes checking for _mingw_mac.h... no

2023-06-10 上传