§1 简介
本章主要内容如下:

❑ UNIX, Linux, and GNU
❑ Linux 程式和程式设计语言
❑ 怎样查找研发资源
❑ 静态和共享库
❑ UNIX哲学
由于和其他linux教程有重复的地方,本章很多地方从简。
§1.1 UNIX, Linux, and GNU 介绍
Linux发展的基础:UNIX and GNU。

* 什么是UNIX(略)

* UNIX简史

UNIX是Open Group的商标,具体见书。主要商业版本有IBM’s
AIX, HP’s HP-UX, and Sun’s Solaris. 免费的有FreeBSD and Linux. 不同的UNIX系统的兼容是个大问题,Linux and UNIX标准请参考18章。

* UNIX哲学:
典型的UNIX程式和系统的特征如下:
❑ Simplicity: KISS, “Keep
It Small and Simple,”
❑ Focus: 一个程式只做一件事情。
❑ Reusable
Components:比如库
❑ Filters:
❑ Open File Formats:使用ASCII text or XML. 比如ctags
❑ Flexibility: 这个暂不太理解

* 什么是LINUX(略)
* GNU项目和FSF
* Linux发行版本

§1.2 Linux程式设计
常用的程式设计语言如下:
Ada C C
Eiffel Forth
Fortran
Icon Java JavaScript
Lisp Modula 2 Modula 3
Oberon Objective C Pascal
Perl PostScript Prolog
Python Ruby Smalltalk
PHP Tcl/Tk Bourne Shell

* Linux程式(略)

编辑器(略)
* C编辑器

第一个程式:
# cat hello.c

#include
#include

int main()
{

printf("Hello World\n");

exit(0);
}

编译和执行如下:

$ gcc -o hello hello.c
$ ./hello
Hello World
$

研发系统的路标
* 应用程式
* 头文档:
提供常量定义,系统和库函数调用声明。一般位于/usr/include中。/usr/include/sys
and /usr/include/linux,这2个目录的具体作用还不太明白。
指定编译目录:
$ gcc -I/usr/openwin/include fred.c
* 库文档:
预编译函数的集合,一般位于/lib
and /usr/lib,文档名以lib开头
❑ .a 传统,静态库
❑ .so 共享库
指定查找库:
$ gcc -o fred fred.c /usr/lib/libm.a
简单写法:
$ gcc -o fred fred.c –lm 这样会优先选择共享库。
指定目录:
$ gcc -o x11fred -L/usr/openwin/lib
x11fred.c -lX11

* 静态库
创建静态库

# cat fred.c
#include

void fred(int arg)
{

printf("fred: you passed %d\n", arg);
}

cat bill.c
#include

void bill(char *arg)
{

printf("bill: you passed %s\n", arg);
}
编译:
$ gcc -c bill.c fred.c
$ ls *.o
bill.o fred.o

创建头文档:
[root@localhost chapter01]# cat
lib.h
/*

This is lib.h. It declares the functions fred and bill for users
*/

void bill(char *);
void fred(int);

使用主程式调用:
# cat program.c
#include
#include "lib.h"

int main()
{

bill("Hello World");

exit(0);
}

编译主程式
$ gcc -c program.c
$ gcc -o program program.o bill.o
$ ./program
bill: we passed Hello World

压缩:
$ ar crv libfoo.a bill.o fred.o
a - bill.o
a - fred.o

可选步骤:
$ ranlib libfoo.a

使用库:
$ gcc -o program program.o libfoo.a 或:$ gcc –o program program.o –L. –lfoo
$ ./program
bill: we passed Hello World

nm 查看对象,库,可执行程式包含的函数。编译时不会包含整个库,只是头文档和实际要使用的函数。


* 共享库
共享库只保存代码的一份拷贝,和静态库位于同一目录,文档名后缀:.so,比如/lib/libm.so.N,后面的数字表示版本号。
查找目录配置的地方:
cat
/etc/ld.so.conf
include
ld.so.conf.d/*.conf
/usr/X11R6/lib
/usr/lib/qt-3.3/lib

查看程式使用了哪些共享库:
比如:
ldd program
/etc/libcwait.so => /etc/libcwait.so
(0x00111000)
linux-gate.so.1 => (0x00a76000)
libc.so.6 => /lib/tls/libc.so.6
(0x00db1000)
/lib/ld-linux.so.2 =>
/lib/ld-linux.so.2 (0x006f9000)

共享库的具体创建过程这里没有讲述,难道和静态相同?仅仅是修改了文档后缀而已?


§1.3 获取帮助(略)







本文来自ChinaUnix博客,假如查看原文请点:http://blog.chinaunix.net/u/21908/showart_469956.html