②安薇理工大学 ANHUI UNIVERSITY OF SCIENCE TECHNOLOGY Linux开发基础 Development Foundation on Linux Os 方贤进,PhD& Associate Prof 安徽理工大学计算机科学与工程学院
安徽理工大学 计算机科学与工程学院 Linux开发基础 Development Foundation on Linux OS 方贤进, Ph.D & Associate Prof
⑧安薇理工大学 ANHUI UNIVERSITY OFSCIENCE TECHNOLOG Section 2 GNU C/C++ programming
3 Section 2 GNU C/C++ programming
⑧安薇理工大学 ANHUI UNIVERSITY OFSCIENCE TECHNOLOG hat is GNU? The GNU operating system is a complete free software system, upward-compatible with Unix. GNU stands for GNU's Not Unix" richard Stallman made the initial Announcement of the GNU Pro ject in September 1983 Source: http://www.gnu.org/gnu/initial announcement. htm
4 What is GNU? ➢ The GNU operating system is a complete free software system, upward-compatible with Unix. GNU stands for “GNU's Not Unix”. Richard Stallman made the Initial Announcement of the GNU Project in September 1983. Source: http://www.gnu.org/gnu/initialannouncement.html
⑧安薇理工大学 ANHUI UNIVERSITY OFSCIENCE TECHNOLOG What is Free Software? Free software"is a matter of liberty, not price. To understand the concept, you should think of"free"as in"free speech", not as in free beer Free software is a matter of the users' freedom to run, copy distribute, study, change and improve the software. More precisely. it refers to four kinds of freedom for the users of the software a The freedom to run the program, for any purpose(freedom O) a The freedom to study how the program works, and adapt it to your needs(freedom 1). access to the source code is a precondition for this a The freedom to redistribute copies so you can help your neighbor (freedom 2) a The freedom to improve the program, and release your improvements to the public, so that the whole community benefits (freedom 3). Access to the source code is a precondition for this
5 What is Free Software? ➢ “Free software” is a matter of liberty, not price. To understand the concept, you should think of “free” as in “free speech”, not as in “free beer”. ➢ Free software is a matter of the users' freedom to run, copy, distribute, study, change and improve the software. More precisely, it refers to four kinds of freedom, for the users of the software: The freedom to run the program, for any purpose (freedom 0). The freedom to study how the program works, and adapt it to your needs (freedom 1). Access to the source code is a precondition for this. The freedom to redistribute copies so you can help your neighbor (freedom 2). The freedom to improve the program, and release your improvements to the public, so that the whole community benefits (freedom 3). Access to the source code is a precondition for this
⑧安薇理工大学 ANHUI UNIVERSITY OFSCIENCE TECHNOLOG hat is gcc? GCC, the GNU Compiler Collection The GNU Compiler Collection includes front ends for C, C+t, Objective-C, Fortran, Java and Ada, as well as libraries for these languages(libstdc++, libgcj, ). GCC was originally written as the compiler for the GNU operating system. The GNU system was developed to be 100% free software free in the sense that it respects the user's freedom Sourcehttp://gcc.gnu.org
6 What is gcc? ➢ GCC, the GNU Compiler Collection. ➢ The GNU Compiler Collection includes front ends for C, C++, Objective-C, Fortran, Java, and Ada, as well as libraries for these languages (libstdc++, libgcj,...). GCC was originally written as the compiler for the GNU operating system. The GNU system was developed to be 100% free software, free in the sense that it respects the user's freedom. Source: http://gcc.gnu.org
⑧安薇理工大学 ANHUI UNIVERSITY OFSCIENCE TECHNOLOG 21 GNU C/C++编译器的使用 gc通过扩展名来判断文件的类型,从而确定用何种方式处理该文件 后缀名 说明 C语言源代码文件 C/cxc+源代码文件 h 程序所包含的头文件 已经预处理过的C语言源代码文件 已经预处理过的C++源代码文件 m Objective-C语言源代码文件 ss 汇编语言源代码文件 经过预编译的汇编语言源代码文件 a/。so 编译后的库代码 编译后的目标文件
7 后缀名 说明 .c C语言源代码文件 .C/.cc/.cxx C++源代码文件 .h 程序所包含的头文件 .i 已经预处理过的C语言源代码文件 .ii 已经预处理过的C++源代码文件 .m Objective-C语言源代码文件 .s 汇编语言源代码文件 .S 经过预编译的汇编语言源代码文件 .a/.so 编译后的库代码 .o 编译后的目标文件 2.1 GNU C/C++编译器的使用 gcc通过扩展名来判断文件的类型,从而确定用何种方式处理该文件
⑧安薇理工大学 ANHUI UNIVERSITY OFSCIENCE TECHNOLOG 21 GNU C/C++编译器的使用 gcc编译器选项和参数 参数 含义 -o Place the output into C Compile and assemble, but do not 99 db Produce debugging information for use by GDB O the compiler tries to reduce code size and execution time without performing any optimizations that take a great deal of com-pilaTion Time Produce debugging information in the operating systems native format. GDB can work with this debugging information I Add the directory dir to the list of directories to be searched for header files. Directories named by-i are searched before the L Search the library named library when linking
8 2.1 GNU C/C++编译器的使用 gcc 编译器选项和参数 参数 含义 -o Place the output into -c Compile and assemble, but do not link -ggdb Produce debugging information for use by GDB. -O the compiler tries to reduce code size and execution time, without performing any optimizations that take a great deal of com-pilation time. -g Produce debugging information in the operating system's native format. GDB can work with this debugging information. -I Add the directory dir to the list of directories to be searched for header files. Directories named by -I are searched before the -L Search the library named library when linking
⑧安薇理工大学 ANHUI UNIVERSITY OFSCIENCE TECHNOLOG 21 GNU C/C++编译器的使用 1、直接通过编译生成目标代码可执行文件: Sgcc hello. c -o hello 2、如果一个程序包含有多个源文件,则也可直接生成目标代码: void f10 printf("%sIn","function 1); void f20 printf( %sIn", function 2); maino f10 f20 Sgcc f1.c f2.c main. c -o main
9 2.1 GNU C/C++编译器的使用 1、直接通过编译生成目标代码可执行文件: $gcc hello.c –o hello 2、如果一个程序包含有多个源文件,则也可直接生成目标代码: void f1() { printf(“%s\n”, "function 1"); } void f2() { printf("%s\n", "function 2"); } main() { f1(); f2(); } $gcc f1.c f2.c main.c –o main
⑧安薇理工大学 ANHUI UNIVERSITY OFSCIENCE TECHNOLOG > Linux上广泛使用的C语言编译器是GNUC编译器,GNU c建立在自由软件基金会的编程许可证的基础上,可以自 由发布。在 Linux下,一个完整的c语言开发环境到少包 括以下三个组成部分: √函数库gbc(在/us/ib和/ib目录中) √编译器gcc √系统头文件q! ibc header(h) glibc是构成一个完整的C语言开发环境所必不可少的组成 邗分,也是Lnux下C语言的主要函数库。 glibc_header中包含了系统编译源代码所需要的声明文 件如果缺少系统头文件,很多用到系统功能的c程序将无 法编译。(包含在/usr/ include/及其子目录中) 10
10 ➢ Linux上广泛使用的C语言编译器是GNU C编译器,GNU C建立在自由软件基金会的编程许可证的基础上,可以自 由发布。在Linux下,一个完整的C语言开发环境到少包 括以下三个组成部分: ✓函数库glibc (在/usr/lib和/lib目录中) ✓编译器gcc ✓系统头文件glibc_header (*.h) ➢ glibc是构成一个完整的C语言开发环境所必不可少的组成 部分,也是Linux下C语言的主要函数库。 ➢ glibc_header中包含了系统编译源代码所需要的声明文 件,如果缺少系统头文件,很多用到系统功能的C程序将无 法编译。(包含在/usr/include/及其子目录中)
⑧安薇理工大学 ANHUI UNIVERSITY OFSCIENCE TECHNOLOG 2.2GNUC/C++函数库 定义:是一些预先编译好的函数的集合,那些函数都是按照可再使用 的原则编写的。它们通常是一组相互关联的用来完成某项常见工作的 函数构成(比如c库里面的标准辅入输出函数、时间函数和数学函数 等)。 函数库中的函数可以通过连接程序与应用程序进行连接,而不必在每 次开发程序时都对这些通用的函数进行编译 不同类型的应用程序将会使用不同的函数库。如数学应用将使用数学 库 Elibm(usr/ib/ibma,/usr/ib/ibm.so),标准的C库 libc(usr/ib/ibc.a, /usr/lib/libc. so) 注意:*h只是对函数的声明( declaration),函数的定义是在具体的函 数库中
11 2.2 GNU C/C++函数库 ➢ 定义:是一些预先编译好的函数的集合,那些函数都是按照可再使用 的原则编写的。它们通常是一组相互关联的用来完成某项常见工作的 函数构成(比如c库里面的标准输入输出函数、时间函数和数学函数 等)。 ➢ 函数库中的函数可以通过连接程序与应用程序进行连接,而不必在每 次开发程序时都对这些通用的函数进行编译。 ➢ 不同类型的应用程序将会使用不同的函数库。如数学应用将使用数学 库libm(/usr/lib/libm.a, /usr/lib/libm.so),标准的C库 libc(/usr/lib/libc.a,/usr/lib/libc.so)。 注意:*.h只是对函数的声明(declaration),函数的定义是在具体的函 数库中