免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1329 | 回复: 0
打印 上一主题 下一主题

《Linux 程序设计入门》 第四版 §1 简介 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-01-23 14:28 |只看该作者 |倒序浏览

§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
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP