linux programming prerequisite

21
Linux Programming Prerequisite Jianjian SONG Software Institute, Nanjing University Sept. 2004

Upload: deion

Post on 02-Feb-2016

88 views

Category:

Documents


1 download

DESCRIPTION

Jianjian SONG Software Institute, Nanjing University Sept. 2004. Linux Programming Prerequisite. Contents. Overview of Linux Programming Using gcc & gdb Make & Makefile. Programming Language. High-level Language C/C++, Java, Fortran… ELF binary format Excutable and Linkable Format - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Linux Programming Prerequisite

Linux Programming Prerequisite

Jianjian SONGSoftware Institute, Nanjing UniversitySept. 2004

Page 2: Linux Programming Prerequisite

Contents

Overview of Linux Programming Using gcc & gdb Make & Makefile

Page 3: Linux Programming Prerequisite

Programming Language

High-level Language C/C++, Java, Fortran… ELF binary format

Excutable and Linkable Format 工具接口标准委员会 (TIS) 选择了正在发展中的 ELF 体系

上不同操作系统之间可移植的二进制文件格式Script

Shell: sh/bash, csh, ksh Perl, Python, tcl/tk, sed, awk…

Page 4: Linux Programming Prerequisite

Development ToolsGCC

GNU C Compiler -> GNU Compiler Collection The gcc command: Front end

GDB GNU Debugger The gdb command xxdgb, ddd…

Binary utilities as, ld, ar, ldd…

Make

Page 5: Linux Programming Prerequisite

Workbench

IDE Emacs/xemacs Kdevelop Eclipse Kylix3

Command line Editor: vi/vim/gvim, emacs/xemacs, pico Source Reader: source navigator; vi/emacs+

ctags/etags Configure Tools: automake, autoconf, m4

Page 6: Linux Programming Prerequisite

文件子系统

用户

硬 件 控 制

硬 件

内核

Shell

用户

用户

用户

高级语言和实用程序

系统调用

进程管理子系统

进程间通信

存储管理

调度程序

设备驱动程序字符设备 块设

高速缓存

核外程序

A User’s Viewpoint

Page 7: Linux Programming Prerequisite

A C Programmer’s Viewpoint

Page 8: Linux Programming Prerequisite

System Calls and Libraries

系统调用 Linux 内核的对外接口;用户程序和内核之

间唯一的接口 函数库

依赖于系统调用 一般来说,标准函数库建立在系统调用的上

层,提供的功能比系统调用强,使用也比较方便。

例:标准 I/O 库

Page 9: Linux Programming Prerequisite

Libraries and Head Files

Static Libraries (.a files) Lab (gcc + ar)

Dynamic Libraries/Shared Objects (.so files) Lab (gcc)

Page 10: Linux Programming Prerequisite

GCC GCC:

GNU C Compiler -> GNU Compiler Collection http://gcc.gnu.org

Front ends and back ends Front ends: gcc, g++, gcj, g77, gnat Back ends: support various target

4 stages of gcc Preprocessing (cpp command) Compilation Assembly (as command) Linking (ld command)

Page 11: Linux Programming Prerequisite

File Name Suffix (1)

Assembler code which must be preprocessed

.S

Assembler code.s

C++ header file to be turned into a precompiled header

.H .hh

C or C++ header file to be turned into a precompiled header

.h

C++ source code which should not be preprocessed

.ii

C++ source code which must be preprocessed

.cc .cp .cpp .CPP.c++ .C .cxx

C source code which should not be preprocessed

.i

C source code which must be preprocessed

.c

Page 12: Linux Programming Prerequisite

File Name Suffix (2)

Dynamic library file (shared object).so

Static library file (archive file).a

Object file.o

Page 13: Linux Programming Prerequisite

GCC options (1) Usage:

gcc [options] [filename] Basic options:

-E: 只对源程序进行预处理 ( 调用 cpp 预处理器 ) -S: 只对源程序进行预处理、编译 -c: 执行预处理、编译、汇编而不链接 -o output_file: 指定输出文件名 -g: 产生调试工具必需的符号信息 -O/On: 在程序编译、链接过程中进行优化处理 -Wall: 显示所有的警告信息

Page 14: Linux Programming Prerequisite

GCC options (2)

Basic options: -Idir: 指定额外的头文件搜索路径 -isystem dir -Ldir: 指定额外的库文件搜索路径 -lname: 链接时搜索指定的库文件 -DMACRO[=DEFN]: 定义 MACRO 宏

Page 15: Linux Programming Prerequisite

GDB

GDB: GNU Debug 设置断点 监视变量值 单步执行 修改变量值

Page 16: Linux Programming Prerequisite

gdb commands

不退出 gdb 就执行 makemake

不退出 gdb 就执行 shell 命令shell

中止正在调试的程序kill

执行当前调试的程序run

推出 gdbquit

临时显示表达式的值print

显示表达式的值display

打开要调试的文件file

执行一条语句,是函数则进入函数内部step

执行一条语句但不进入函数内部next

列出源代码的一部分list

设置断点,可以是行号、函数名及地址 ( 以 * 开头 )tbreak: 设置临时断点

break/tbreak

Page 17: Linux Programming Prerequisite

Quiz

从标准输入 (stdin/cin) 输入 10 个 ( 或n 个 , n 不定 ) 整数,排序后打印到标准输出。 C/C++ 均可 注明采用的数据结构、排序算法

Page 18: Linux Programming Prerequisite

make & makefile

Multi-file project IDE make

make & makefile makefile 描述模块间的依赖关系; make 命令根据 makefile 对程序进行管理

和维护; make 判断被维护文件的时序关系

Page 19: Linux Programming Prerequisite

Makefile

Makefile : Dependency(target,

prerequisites) Rule(command(s))

Example :

Page 20: Linux Programming Prerequisite

make

make [-f filename] [targetname] Targets

A target is usually the name of a file that is generated by a program; examples of targets are executable or object files.

A target can also be the name of an action to carry out, such as 'clean' (phony target).

Page 21: Linux Programming Prerequisite

makefile extensions

Makefile extensions macros/variables implicit/suffix rules subdirectories archive

Using "info make"