Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19733027
  • 博文数量: 679
  • 博客积分: 10495
  • 博客等级: 上将
  • 技术积分: 9308
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 10:51
文章分类

全部博文(679)

文章存档

2012年(5)

2011年(38)

2010年(86)

2009年(145)

2008年(170)

2007年(165)

2006年(89)

分类: LINUX

2008-01-23 14:28:31

简介

本章主要内容如下:

 

UNIX, Linux, and GNU

Linux 程序和程序设计语言

怎样查找开发资源

静态和共享库

UNIX哲学

由于和其他linux教程有重复的地方,本章很多地方从简。

§1.1  UNIX, Linux, and GNU 介绍

Linux发展的基础:UNIX and GNU

 

     什么是UNIX()

 

   UNIX简史

 

UNIXOpen 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  获取帮助()

 

 

 

阅读(3699) | 评论(3) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2008-01-26 07:03:10

多多指点阿,我的英文很烂,看起来很慢,最近又在台湾出差,垃圾事情忙得要死

yangbajing2008-01-25 11:36:54

Flexibility 灵活性 楼主在看这本书啊,我也正在学。

chinaunix网友2008-01-24 06:50:34

good