v v v GNU CC GNU Compiler Collection

  • Slides: 55
Download presentation
编程 具包 v v v GNU CC (GNU Compiler Collection) GNU make 管理项目 GDB

编程 具包 v v v GNU CC (GNU Compiler Collection) GNU make 管理项目 GDB (GNU De. Bugger)调试 6/17/2021 Linux及编程 1

GNU CC v 实例 6/17/2021 Linux及编程 4

GNU CC v 实例 6/17/2021 Linux及编程 4

GNU CC v 对文件扩展名的解释 ——For any given input file, the file name surfix determines

GNU CC v 对文件扩展名的解释 ——For any given input file, the file name surfix determines what kind of compilation is done. file. c C source code which must be preprocessed. file. i C source code which should not be preprocessed. file. ii C++ source code which should not be preprocessed. file. h C or C++ header le to be turned into a precompiled header. file. cc, file. cp, file. cxx, file. cpp, file. CPP, file. c++, file. C C++ source code which must be preprocessed. Note that in. cxx, the last two letters must both be literally x. Likewise, . C refers to a literal capital C. file. hh, file. H C++ header file to be turned into a precompiled header. file. s Assembler code. file. S Assembler code which must be preprocessed. 6/17/2021 Linux及编程 7

GNU CC v 常用的命令行选项 -x language Specify explicitly the language for the following input

GNU CC v 常用的命令行选项 -x language Specify explicitly the language for the following input files (rather than letting the compiler choose a default based on the file name suffix). This option applies to all following input files until the next -x option. Possible values for language are: vc c-header cpp-output v c++-header c++-cpp-output v assembler-with-cpp -x none Turn off any specification of a language, so that subsequent files are handled according to their file name suffixes (as they are if -x has not been used at all). 6/17/2021 Linux及编程 8

GNU CC v 常用的命令行选项 ——-x和下述几个选项结合实现分步编译 -c Compile or assemble the source files, but do

GNU CC v 常用的命令行选项 ——-x和下述几个选项结合实现分步编译 -c Compile or assemble the source files, but do not link. -S Stop after the stage of compilation proper; do not assemble. -E Stop after the preprocessing stage; do not run the compiler proper. -o file Place output in file. v If -o is not specified, the default is to put an executable le in a. out, the object file for source. suffix in source. o, its assembler file in source. s, and all preprocessed C source on standard output. -v Print (on standard error output) the commands executed to run the stages of compilation. -### Like -v except the commands are not executed and all command arguments are quoted. This is useful for shell scripts to capture the driver-generated command lines. Note that some combinations (for example, -x cpp-output -E) instruct gcc to do nothing at all. 6/17/2021 Linux及编程 9

GNU CC v 常用的命令行选项 ——下述选项控制C(C++)的“方言” In C mode, support all ISO C 89 programs.

GNU CC v 常用的命令行选项 ——下述选项控制C(C++)的“方言” In C mode, support all ISO C 89 programs. In C++ mode, remove GNU extensions that conflict with ISO C++. -std= Determine the language standard. v c 89,iso 9899: 1990 ISO C 90 (same as -ansi). v iso 9899: 199409 ISO C 90 as modied in amendment 1. v c 99,c 9 x,iso 9899: 1999,iso 9899: 199 x ISO C 99. v gnu 89 Default, ISO C 90 plus GNU extensions (including some C 99 features). v gnu 99,gnu 9 x ISO C 99 plus GNU extensions. When ISO C 99 is fully implemented in GCC, this will become the default. The name gnu 9 x is deprecated. v c++98 The 1998 ISO C++ standard plus amendments. v gnu++98 The same as -std=c++98 plus GNU extensions. This is the default for C++ code. -ansi 6/17/2021 Linux及编程 10

GNU CC v 常用的命令行选项 ——警告信息 Can request many specific warnings with options beginning –W.

GNU CC v 常用的命令行选项 ——警告信息 Can request many specific warnings with options beginning –W. -fsyntax-only Check the code for syntax errors, but don't do anything beyond that. -pedantic Issue all the warnings demanded by strict ISO C and ISO C. -pedantic-errors Like -pedantic, except that errors are produced rather than warnings. -w Inhibit all warning messages. -Wchar-subscripts Warn if an array subscript has type char. -Wcomment Warn whenever 注释嵌套. -Wformat Check calls to printf and scanf, etc. , to make sure that the arguments supplied have types appropriate to the format string specified, and that the conversions specified in the format string make sense. 6/17/2021 Linux及编程 11

GNU CC v 常用的命令行选项 ——警告信息 -Wnonnull Enable warning about passing a null pointer for

GNU CC v 常用的命令行选项 ——警告信息 -Wnonnull Enable warning about passing a null pointer for arguments marked as requiring a non-null value by the nonnull function attribute. -Wimplicit-int Warn when a declaration does not specify a type. -Wimplicit-function-declaration -Werror-implicit-function-declaration Give a warning (or error) whenever a function is used before being declared. -Wmain Warn if the type of main is suspicious. -Wmissing-braces Warn if an aggregate or union initializer is not fully bracketed. For example, int a[2][2] = { 0, 1, 2, 3 }. -Wparentheses Warn if parentheses are omitted in certain contexts, such as when there is an assignment in a context where a truth value is expected. E. g. if(a=2){…}. 6/17/2021 Linux及编程 12

GNU CC v 常用的命令行选项 ——警告信息 -Wreturn-type Warn whenever a function is defined with a

GNU CC v 常用的命令行选项 ——警告信息 -Wreturn-type Warn whenever a function is defined with a return-type that defaults to int. -Wswitch Warn whenever a switch statement has an index of enumeral type and lacks a case for one or more of the named codes of that enumeration. -Wswitch-default Warn whenever a switch statement does not have a default case. -Wunused-function Warn whenever a static function is declared but not defined or a non-inline static function is unused. -Wunused-label Warn whenever a label is declared but not used. -Wunused-parameter Warn whenever a function parameter is unused aside from its declaration. -Wunused-variable Warn whenever a local variable or nonconstant static variable is unused aside from its declaration. -Wunused All the above -Wunused options combined. 6/17/2021 Linux及编程 13

GNU CC v 常用的命令行选项 ——警告信息 -Wuninitialized Warn if an automatic variable is used without

GNU CC v 常用的命令行选项 ——警告信息 -Wuninitialized Warn if an automatic variable is used without rist being initialized or if a variable may be clobbered by a set jmp call. -Wall All of the above -W options combined. -Wsystem-headers Print warning messages for constructs found in system header files. -Wfloat-equal Warn if floating point values are used in equality comparisons. -Wundef Warn if an undefined identifier is evaluated in an #if directive. -Wunreachable-code Warn if the compiler detects that code will never be executed. -Winline Warn if a function can not be inlined and it was declared as inline. -Werror Make all warnings into errors. 6/17/2021 Linux及编程 14

GNU CC v 常用的命令行选项 ——调试选项 -g Produce debugging information in the operating system's native

GNU CC v 常用的命令行选项 ——调试选项 -g Produce debugging information in the operating system's native format (stabs, COFF, XCOFF, or DWARF). GDB can work with this debugging information. -ggdb Produce debugging information for use by GDB. -ftime-report Makes the compiler print some statistics about the time consumed by each pass when it finishes. -fmem-report Makes the compiler print some statistics about permanent memory allocation when it finishes. 6/17/2021 Linux及编程 20

GNU CC v 常用的命令行选项 ——调试选项 -O 1 Optimizing compilation takes somewhat more time, and

GNU CC v 常用的命令行选项 ——调试选项 -O 1 Optimizing compilation takes somewhat more time, and a lot more memory for a large function. With -O, the compiler tries to reduce code size and execution time, without performing any optimizations that take a great deal of compilation time. -O 2 Optimize even more. -O 3 Optimize yet more. -O 0 Do not optimize. This is the default. -Os Optimize for size. 6/17/2021 Linux及编程 21

GNU CC v 常用的命令行选项 ——控制预处理的选项 -D name Predefine name as a macro, with definition

GNU CC v 常用的命令行选项 ——控制预处理的选项 -D name Predefine name as a macro, with definition 1. -D name=definition Predefine name as a macro. -U name Cancel any previous definition of name, either built in or provided with a -D option. -undef Do not predefine any system-specific or GCC-specific macros. The standard predefined macros remain defined. 6/17/2021 Linux及编程 22

GNU CC v 常用的命令行选项 ——控制文件搜索路径 -I dir Add the directory dir to the list

GNU CC v 常用的命令行选项 ——控制文件搜索路径 -I dir Add the directory dir to the list of directories to be searched for header files. Directories named by -I are searched before the standard system include directories. -llibrary -L library Search the library named library (liblibrary. a) when linking. 6/17/2021 Linux及编程 23

GNU CC v 常用的命令行选项 ——体系结构相关 -mtune=cpu-type Tune to cpu-type everything applicable about the generated

GNU CC v 常用的命令行选项 ——体系结构相关 -mtune=cpu-type Tune to cpu-type everything applicable about the generated code, except for the ABI and the set of available instructions. The choices for cpu-type are i 386, i 486, i 586, i 686, pentium-mmx, pentiumpro, pentium 2, pentium 3, pentium 4, k 6 -2, k 6 -3, athlon-tbird, athlon-4, athlonxp, athlon-mp, winchip-c 6, winchip 2, k 8, c 3 and c 3 -2. . -march=cpu-type Generate instructions for the machine type cpu-type. -mieee-fp -mno-ieee-fp Control whether or not the compiler uses IEEE floating point comparisons. -m 32 -m 64 Generate code for a 32 -bit or 64 -bit environment. 6/17/2021 Linux及编程 24

GNU make 管理项目 v 例子 /* * helper. h - Header for helper. c

GNU make 管理项目 v 例子 /* * helper. h - Header for helper. c */ void msg(void); /* * helper. c - Helper code for howdy. c */ #include <stdio. h> #include "helper. h" void msg(void) { printf("This message sent from Jupiter. n"); } 6/17/2021 Linux及编程 28

GNU make 管理项目 v 例子 /* * hello. c - Canonical "Hello, World!" program

GNU make 管理项目 v 例子 /* * hello. c - Canonical "Hello, World!" program */ #include <stdio. h> #include "helper. h" int main(void) { printf("Hello, Linux programming world!n"); msg(); return 0; } 6/17/2021 Linux及编程 29

GNU make 管理项目 v 默 认 目 标 体 , m a k e

GNU make 管理项目 v 默 认 目 标 体 , m a k e 要 创 建 的 文 件 例子 注释 # Sample Makefile for make 避免编译器调用未声明的函数产生出错信息 howdy: howdy. o helper. h gcc howdy. o helper. o -o howdy helper. o: helper. c helper. h gcc -c helper. c 三个依赖体 命令 howdy. o: howdy. c gcc -c howdy. c 怎样生成单个目标 hello: hello. c gcc hello. c -o hello all: howdy hello clean: rm howdy hello *. o 6/17/2021 笼统规则 伪目标,没有依赖体,因此认为目标体是最新 的,不会被自动执行 Linux及编程 30

GNU make 管理项目 v 例子 # Sample 2 Makefile for make howdy: howdy. o

GNU make 管理项目 v 例子 # Sample 2 Makefile for make howdy: howdy. o helper. h gcc howdy. o helper. o -o howdy helper. o: helper. c helper. h gcc -c helper. c howdy. o: howdy. c gcc -c howdy. c hello: hello. c gcc hello. c -o hello 不检查是否存在有文件名和依赖体中的一个名字相匹配的 文件,而是直接执行与之相关的命令。 all: howdy hello. PHONY: clean: rm howdy hello *. o 6/17/2021 Linux及编程 33

GNU make 管理项目 v 例子 6/17/2021 Linux及编程 35

GNU make 管理项目 v 例子 6/17/2021 Linux及编程 35

GNU make 管理项目 v 变量使用例子 1 objects = program. o foo. o utils. o

GNU make 管理项目 v 变量使用例子 1 objects = program. o foo. o utils. o program : $(objects) cc -o program $(objects) : defs. h v 变量使用例子 2 foo = c prog. o : prog. $(foo) -$(foo) prog. $(foo) 6/17/2021 Linux及编程 37

GNU make 管理项目 v 两种变量定义 递归展开变量 #Makefile 3 foo = $(bar) bar = $(ugh)

GNU make 管理项目 v 两种变量定义 递归展开变量 #Makefile 3 foo = $(bar) bar = $(ugh) ugh = Huh? all: ; echo $(foo) 简单展开变量 x : = foo y : = $(x) bar x : = later 就等价于: y : = foo bar x : = later 6/17/2021 Linux及编程 38

GNU make 管理项目 v make出错信息 [FOO] Error NN [FOO] signal description v 这类错误并不是make的真正错误。它表示make检测到make所调用的作为执 行命令的程序返回一个非零状态

GNU make 管理项目 v make出错信息 [FOO] Error NN [FOO] signal description v 这类错误并不是make的真正错误。它表示make检测到make所调用的作为执 行命令的程序返回一个非零状态 missing separator. Stop. missing separator (did you mean TAB instead of 8 spaces? ). Stop. v 不可识别的命令行,make在读取Makefile过程中不能解析其中包含的内容。 commands commence before first target. Stop. missing rule before commands. Stop. v Makefile可能是以命令行开始:以[Tab]字符开始,但不是一个合法的命令行 (例如,一个变量的赋值)。命令行必须和规则一一对应。 No rule to make target `XXX'. No rule to make target ` XXX ', needed by `yyy'. v 无法为重建目标“XXX”找到合适的规则,包括明确规则和隐含规则。 6/17/2021 Linux及编程 43

GNU make 管理项目 v make出错信息 Recursive variable `XXX' references itself (eventually). Stop. v make的变量“XXX”(递归展开式)在替换展开时,引用它自身。

GNU make 管理项目 v make出错信息 Recursive variable `XXX' references itself (eventually). Stop. v make的变量“XXX”(递归展开式)在替换展开时,引用它自身。 Unterminated variable reference. Stop. v 变量或者函数引用语法不正确,没有使用完整的的括号(缺少左括号或者右括 号)。 ‘target’ is up to date v 指定‘target’的相关文件没有变化 6/17/2021 Linux及编程 45

GDB 调试 v 采用的例子程序 / * debugme. c - Poorly written program to debug

GDB 调试 v 采用的例子程序 / * debugme. c - Poorly written program to debug */ #include <stdio. h> #include <stdlib. h> #define BIGNUM 5000 void index_to_the_moon(int ary[]); int main(void) { intary[100]; index_to_the_moon(intary); exit(EXIT_SUCCESS); } void index_to_the_moon(int ary[]) { int i; for(i = 0; i < BIGNUM; ++i) ary[i] = i; } 6/17/2021 Linux及编程 47

GDB 调试 v 使用GDB基本命令 设置断点 (gdb)break linenum (gdb)break funcnum (gdb)break filename: linenum (gdb)break filename:

GDB 调试 v 使用GDB基本命令 设置断点 (gdb)break linenum (gdb)break funcnum (gdb)break filename: linenum (gdb)break filename: funcnum (gdb)break linenum if expr (gdb)break funcnum if expr (gdb)Info breakpoints 察看设置的断点 (gdb)delete n 删除n号断点 (gdb)disable m 使断点无效 6/17/2021 Linux及编程 53

GDB 调试 v 使用GDB基本命令 设置断点 (gdb)break linenum (gdb)break funcnum (gdb)break filename: linenum (gdb)break filename:

GDB 调试 v 使用GDB基本命令 设置断点 (gdb)break linenum (gdb)break funcnum (gdb)break filename: linenum (gdb)break filename: funcnum (gdb)break linenum if expr (gdb)break funcnum if expr (gdb)Info breakpoints 察看设置的断点 (gdb)delete n 删除n号断点 (gdb)disable m 使断点无效 6/17/2021 Linux及编程 54