Linux应用程序开发Word下载.docx
- 文档编号:18890492
- 上传时间:2023-01-02
- 格式:DOCX
- 页数:18
- 大小:23.25KB
Linux应用程序开发Word下载.docx
《Linux应用程序开发Word下载.docx》由会员分享,可在线阅读,更多相关《Linux应用程序开发Word下载.docx(18页珍藏版)》请在冰豆网上搜索。
头文件,位于/usr/include目录。
头文件包含有常量定义、系统调用和库函数调用的声明。
这是系统默认的头文件存放路径,在编译程序时,编译器会自动查找该目录。
gcc编译器在编译程序时也可用-I参数指定另外的头文件路径。
如:
gcc-I/usr/local/myincludetest.c。
库文件,库是一组已编译的函数集合,可方便我们重用代码。
默认存放在/lib和/usr/lib目录。
库文件可分为静态和共享两类。
∙.a,静态库文件。
使用静态库将会把所有的库代码引入程序,占用更多的磁盘空间和内存空间,所以一般建议使用共享库。
∙.so,共享库文件。
使用共享库的程序不包含库代码,只在程序运行才调用共享库中的代码。
在编译时可用包含路径的库文件名或用-l参数指定使用的库文件,/usr/lib/libm.a等价于-lm。
gcc-ohellohello.c/usr/lib/libm.a
或用-l参数写成
gcc-ohellohello.c-lm
如果我们要使用的库文件不在默认位置,在编译程序时可用-L参数指定库文件的路径。
下面例子使用了/usr/hello/lib目录下的libhello库文件:
gcc-ohello-L/usr/hello/libhello.c-lhello
创建和使用静态库。
∙分别创建两个函数,函数a的内容如下:
∙#include<
stdio.h>
∙
∙voida(char*arg)
∙{
∙printf("
functiona,helloworld%s\n"
arg);
∙}
函数b的内容如下:
#include<
voidb(intarg)
{
printf("
functionb,helloworld%d\n"
}
∙接着,生成两个对象文件。
∙debian:
~/c#gcc-ca.cb.c
~/c#ls*.o
∙a.ob.o
∙最后,用ar归档命令把生成的对象文件打包成一个静态库libhello.a。
~/c#arcrvlibhello.aa.ob.o
∙r-a.o
∙r-b.o
∙为我们的静态库定义一个头文件lib.h,包含这两个函数的定义。
∙/*
∙*thisisaheaderfile.
∙*/
∙voida(char*arg);
∙voidb(intarg);
∙}}}
∙*创建jims.c程序,内容如下。
{{{#!
cplusplus
∙#include"
lib.h"
∙intmain()
∙a("
jims.yang"
);
∙b(3);
∙exit(0);
∙利用静态链接库编译程序。
~/c#gcc-cjims.c
~/c#gcc-ojimsjims.olibhello.a
~/c#./jims
∙functiona,helloworldjims.yang
∙functionb,helloworld3
~/c#
gcc-ojimsjims.olibhello.a也可以写成gcc-ojimsjims.o-L.-lhello。
预处理,在程序开头以“#”开头的命令就是预处理命令,它在语法扫描和分析法时被预处理程序处理。
预处理有以下几类:
∙宏定义,用#define指令定义。
#defineBUFFER1024。
取消宏定义用#undef指令。
宏还可带参数,如:
∙#defineBUF(x)x*3
∙包含头文件,用#include指令,可把包含的文件代码插入当前位置。
∙<
包含的文件可以用尖括号,也可用双引号,如:
#include"
stdio.h"
不同之处是,使用尖括号表示在系统的包含目录(/usr/include)下查找该文件,而双引号表示在当前目录下查找包含文件。
每行只能包含一个包含文件,要包含多个文件要用多个#include指令。
∙条件编译,格式如下:
∙格式一,如果定义了标识符,则编译程序段1,否则编译程序段2:
∙#ifdef标识符
∙程序段1
∙#else
∙程序段2
∙#endif
∙格式二,如果定义了标识符,则编译程序段2,否则编译程序段1,与格式一相反:
∙#ifndef标识符
∙#endif
∙格式三,常量表达式为真则编译程序段1,否则编译程序段2:
∙#if常量表达式
使用gcc编译程序时,要经过四个步骤。
∙预处理(Pre-Processing),用-E参数可以生成预处理后的文件。
~/c#gcc-Ehello.c-ohello.i
∙编译(Compiling)
∙汇编(Assembling)
∙链接(Linking)
GCC默认将.i文件看成是预处理后的C语言源代码,所以我们可以这样把.i文件编译成目标文件。
debian:
~#gcc-chello.i-ohello.o}}}
在GCC中使用-pedantic选项能够帮助程序员发现一些不符合ANSI/ISOC标准的代码,但不是全部。
从程序员的角度看,函数库实际上就是一些头文件(.h)和库文件(.so或者.a)的集合。
Chapter3.文件处理
在Linux系统内所有东西都是以文件的形式来表示的,除一般的磁盘文件外,还有设备文件,如硬盘、声卡、串口、打印机等。
设备文件又可分为字符设备文件(characterdevices)和块设备文件(blockdevices)。
使用manhier命令可以查看Linux文件系统的分层结构。
文件的处理方法一般有五种,分别是:
∙open,打开一个文件或设备。
∙close,关闭一个打开的文件或设备。
∙read,从一个打开的文件或者设备中读取信息。
∙write,写入一个文件或设备。
∙ioctl,把控制信息传递给设备驱动程序。
open,close,read,write和ioctl都是低级,没有缓冲的文件操作函数,在实际程序开发中较少使用,一般我们使用标准I/O函数库来处理文件操作。
fopen,fclose,fread,fwrite,fflush等。
在使用标准I/O库时,需用到stdio.h头文件。
一些常用的文件和目录维护函数:
chmod、chown、unlink、link、symlink、mkdir、rmdir、chdir、getcwd、opendir,closedir、readdir、telldir、seekdir等。
fcntl用于维护文件描述符,mmap用于分享内存。
创建文档并输入信息的示例代码:
main(void)
{
FILE*fp1;
charc;
fp1=fopen("
text.txt"
"
w"
while((c=getchar())!
='
\n'
)
putc(c,fp1);
fclose(fp1);
}
显示路径的示例代码
unistd.h>
dirent.h>
string.h>
sys/stat.h>
stdlib.h>
intmain(intargc,char*argv[])
char*topdir="
."
;
if(argc>
=2)
topdir=argv[1];
Directoryscanof%s\n"
topdir);
printdir(topdir,0);
done.\n"
exit(0);
printdir(char*dir,intdepth)
DIR*dp;
structdirent*entry;
structstatstatbuf;
if((dp=opendir(dir))==NULL)
fprintf(stderr,"
cannotopendirectory:
%s\n"
dir);
return;
chdir(dir);
while((entry=readdir(dp))!
=NULL)
lstat(entry->
d_name,&
statbuf);
if(S_ISDIR(statbuf.st_mode))
if(strcmp("
entry->
d_name)==0||strcmp("
.."
d_name)==0)
continue;
%*s%s/\n"
depth,"
"
d_name);
printdir(entry->
d_name,depth+4);
elseprintf("
%*s%s\n"
chdir("
closedir(dp);
Chapter4.Linux环境
voidmain()表示程序没有参数,intmain(intargc,char*argv[])表示程序要带参数,argc保存着参数的个数,argv[]数组保存着参数列表。
~#mytestabc
argc:
4
argv:
["
mytest"
a"
b"
c"
]
getopt()函数和getopt_long()用来处理程序选项。
getopt_long()函数可以处理以"
--"
开头的选项。
Gnu官方手册页:
http:
//www.gnu.org/software/libc/manual/html_node/Getopt.html
获取命令行参数的示例代码:
intopt;
while((opt=getopt(argc,argv,"
if:
lr"
))!
=-1)/*返回“-1”表示已没选项需要处理。
*/
switch(opt){
case'
i'
:
l'
r'
option:
%c\n"
opt);
break;
f'
filename:
%s\n"
optarg);
/*如果选项需要一个参数,则参数存放在外部变量optarg中。
'
optionneedsavalue\n"
/*“:
”表示选项需要参数*/
?
unknownoption:
optopt);
/*返回“?
”表示无效的选项,并把无效的选项存放在外部变量optopt中。
for(;
optind<
argc;
optind++)/*外部变量optind指向下一个要处理的选项索引值。
argument:
argv[optind]);
在bashshell中使用set命令可以列出Linux系统的环境变量,在C程序中我们也可以用putenv()和getenv()函数来获取Linux系统的环境变量。
这两个函数的声明如下:
char*getenv(constchar*name);
intputenv(constchar*string);
系统有一个environ变量记录了所有的系统变量。
下面的示例代码可把environ的值显示同来。
externchar**environ;
intmain()
char**env=environ;
while(*env)
*env);
env++;
linux和其它unix一样,使用GMT1970年1月1日子夜作为系统时间的开始,也叫UNIX纪元的开始。
现在的时间表示为UNIX纪元至今经过的秒数。
time.h>
time_ttime(time_t*t);
显示系统时间的示例代码:
inti;
time_tthe_time;
for(i=1;
i<
=10;
i++){
the_time=time((time_t*)0);
%dthetimeis%ld\n"
i,the_time);
sleep
(2);
用ctime()函数以友好方式返回当前时间,它的函数声明格式:
char*ctime(consttime_t*timeval);
示例:
intmain(void)
time_ttime1;
(void)time(&
time1);
Thedateis:
ctime(&
time1));
用mkstemp()函数创建临时文件。
声明:
#include<
intmkstemp(char*template);
chartemplate[]="
template-XXXXXX"
intfp;
fp=mkstemp(template);
template=%s\n"
template);
close(fp);
获取用户信息。
sys/types.h>
pwd.h>
structpasswd*getpwuid(uid_tuid);
/*根据uid返回用户信息*/
structpasswd*getpwnam(constchar*name);
/*根据用户名返回用户信息*/
passwd结构体说明:
passwdMemberDescription
char*pw_nameTheuser'
sloginname
uid_tpw_uidTheUIDnumber
gid_tpw_gidTheGIDnumber
char*pw_dirTheuser'
shomedirectory
char*pw_gecosTheuser'
sfullname
char*pw_shellTheuser'
sdefaultshell
示例代码:
uid_tuid;
gid_tgid;
structpasswd*pw;
uid=getuid();
gid=getgid();
pw=getpwuid(uid);
Useris%s\n"
getlogin());
Theuidis:
%d\n"
uid);
Thegidis:
gid);
Thepwstruct:
\nname=%s,uid=%d,gid=%d,home=%s,shell=%s\n"
pw->
pw_name,pw->
pw_uid,pw->
pw_gid,pw->
pw_dir,pw->
pw_shell);
用gethostname()函数获取主机名。
函数声明:
intgethostname(char*name,size_tnamelen);
/*主机名返回给name变量*/
charcomputer[100];
intstatus;
status=gethostname(computer,100);
Thestatusis%d\n"
status);
Thehostnameis:
computer);
用uname()函数获取主机详细信息,就像shell的uname命令返回的信息一样。
sys/utsname.h>
intuname(structutsname*name);
utsname结构体说明:
utsnameMemberDescription
charsysname[]Theoperatingsystemname
charnodename[]Thehostname
charrelease[]Thereleaselevelofthesystem
charversion[]Theversionnumberofthesystem
charmachine[]Thehardwaretype
structutsnameuts;
status=gethostname(computer,100);
Thecomputer'
ssizeis%d\n"
sizeof(computer));
uname(&
uts);
Theuname'
sinformation.\nuts.sysname=%s\nuts.machine=%s\nuts.nodename=%s\nuts.release=%s\nuts.version=%s\n"
uts.sysname,uts.machine,uts.nodename,uts.release,uts.version);
使用syslog()函数处理日志信息。
syslog.h>
voidsyslog(intpriority,constchar*message,arguments...);
priority参数的格式(severitylevel|facilitycode)
LOG_ERR|LOG_USER
severitylevel:
PriorityLevelDescription
LOG_EMERGAnemergencysituation
LOG_ALERTHigh-priorityproblem,suchasdatabasecorruption
LOG_CRITCriticalerror,suchashardwarefailure
LOG_ERRErrors
LOG_WARNINGWarning
LOG_NOTICESpecialconditionsrequiringattention
LOG_INFOInformationalmessages
LOG_DEBUGDebugmessages
facilityvalue(转自syslog.h头文件):
/*facilitycodes*/
#defineLOG_KERN(0<
<
3)/*kern
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Linux 应用程序 开发
![提示](https://static.bdocx.com/images/bang_tan.gif)