如果直接編譯myls.c,會報錯
/tmp/cceQcwN0.o: In function `main':
myls.c:(.text+0x24): undefined reference to `err_quit'
myls.c:(.text+0x5b): undefined reference to `err_sys'
collect2: ld 返回 1
需要下載隨書附帶的源代碼并編譯所需的庫文件
源代碼下載:http://www.aygfsteel.com/Files/zellux/apue.linux3.tar.Z.zip
(下載后去掉.zip后綴)
apue/README 文件中有具體的編譯方法,對于Linux系統比較簡單,進入lib.rhlin目錄,然后運行make,就會在apue目錄下生成libmisc.a,以后編譯的時候連接這個庫就行了。
以書中的第一個程序myls.c為例
$ gcc myls.c libmisc.a
出現幾個Warning:
libmisc.a(strerror.o): In function `strerror':
strerror.c:(.text+0x18): warning: `sys_errlist' is deprecated; use `strerror' or `strerror_r' instead
strerror.c:(.text+0xf): warning: `sys_nerr' is deprecated; use `strerror' or `strerror_r' instead
看來這本書的確太老了
$ ./a.out .
就會列出當前目錄下的所有文件
/tmp/cceQcwN0.o: In function `main':
myls.c:(.text+0x24): undefined reference to `err_quit'
myls.c:(.text+0x5b): undefined reference to `err_sys'
collect2: ld 返回 1
需要下載隨書附帶的源代碼并編譯所需的庫文件
源代碼下載:http://www.aygfsteel.com/Files/zellux/apue.linux3.tar.Z.zip
(下載后去掉.zip后綴)
apue/README 文件中有具體的編譯方法,對于Linux系統比較簡單,進入lib.rhlin目錄,然后運行make,就會在apue目錄下生成libmisc.a,以后編譯的時候連接這個庫就行了。
以書中的第一個程序myls.c為例
#include <sys/types.h>
#include <dirent.h>
#include "ourhdr.h"
int main(int argc, char* argv[])
{
DIR *dp;
struct dirent *dirp;
if (argc != 2)
err_quit("a single argument (the directory name) is required");
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);
}
#include <dirent.h>
#include "ourhdr.h"
int main(int argc, char* argv[])
{
DIR *dp;
struct dirent *dirp;
if (argc != 2)
err_quit("a single argument (the directory name) is required");
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);
}
$ gcc myls.c libmisc.a
出現幾個Warning:
libmisc.a(strerror.o): In function `strerror':
strerror.c:(.text+0x18): warning: `sys_errlist' is deprecated; use `strerror' or `strerror_r' instead
strerror.c:(.text+0xf): warning: `sys_nerr' is deprecated; use `strerror' or `strerror_r' instead
看來這本書的確太老了

$ ./a.out .
就會列出當前目錄下的所有文件