编译unix环境高级编程第一个例子出错解决

unix环境高级编程第一个例子ls.c:

01 #include "apue.h"
02 #include <dirent.h>
03  
04 int
05 main(int argc,char *argv[])
06 {
07         DIR     *dp;
08         struct dirent   *dirp;
09  
10         if(argc != 2)
11                 err_quit("usage: ls directory_name");
12  
13         if((dp = opendir(argv[1])) == NULL)
14                 err_sys("can't open %s", argv[1]);
15         while((dirp = readdir(dp))!=NULL)
16                 printf("%s\n",dirp->d_name);
17         closedir(dp);
18         exit(0);
19 }


编译出错:

[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,解决了。

你可能感兴趣的:(unix)