unix环境高级编程第一个例子ls.c:
#include "apue.h"
#include <dirent.h>
int
main(int argc,char *argv[])
{
DIR *dp;
struct dirent *dirp;
if(argc != 2)
err_quit("usage: ls directory_name");
if((dp = opendir(argv[1])) == NULL)
err_sys("can't open %s", argv[1]);
while((dirp = readdir(dp))!=NULL)
printf("%s\n",dirp->d_name);
closedir(dp);
exit(0);
}
编译出错:
[john@localhost apue]$ gcc ls.c
/tmp/cchIn3xL.o: In function `main':
ls.c:(.text+0x17): undefined reference to `err_quit'
ls.c:(.text+0x4a): undefined reference to `err_sys'
发现是找不到err_quit和err_sys,于是把unix环境高级编程源码的lib目录下的error.c拷贝出来和一起编译:
[john@localhost apue]$ gcc ls.c error.c
OK,解决了。
来源:oschina
链接:https://my.oschina.net/u/96750/blog/80402