unix環境高級編程第一個例子ls.c: shell
#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拷貝出來和一塊兒編譯:
ui
[john@localhost apue]$ gcc ls.c error.cOK,解決了。