編譯《UNIX環(huán)境高級(jí)編程》中的第一個(gè)程序
Posted on 2007-08-03 01:13 ZelluX 閱讀(2235) 評(píng)論(2) 編輯 收藏 所屬分類(lèi): C/C++如果直接編譯myls.c,會(huì)報(bào)錯(cuò)
/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
需要下載隨書(shū)附帶的源代碼并編譯所需的庫(kù)文件
源代碼下載:http://www.aygfsteel.com/Files/zellux/apue.linux3.tar.Z.zip
(下載后去掉.zip后綴)
apue/README 文件中有具體的編譯方法,對(duì)于Linux系統(tǒng)比較簡(jiǎn)單,進(jìn)入lib.rhlin目錄,然后運(yùn)行make,就會(huì)在apue目錄下生成libmisc.a,以后編譯的時(shí)候連接這個(gè)庫(kù)就行了。
以書(shū)中的第一個(gè)程序myls.c為例
$ gcc myls.c libmisc.a
出現(xiàn)幾個(gè)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
看來(lái)這本書(shū)的確太老了
$ ./a.out .
就會(huì)列出當(dāng)前目錄下的所有文件
/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
需要下載隨書(shū)附帶的源代碼并編譯所需的庫(kù)文件
源代碼下載:http://www.aygfsteel.com/Files/zellux/apue.linux3.tar.Z.zip
(下載后去掉.zip后綴)
apue/README 文件中有具體的編譯方法,對(duì)于Linux系統(tǒng)比較簡(jiǎn)單,進(jìn)入lib.rhlin目錄,然后運(yùn)行make,就會(huì)在apue目錄下生成libmisc.a,以后編譯的時(shí)候連接這個(gè)庫(kù)就行了。
以書(shū)中的第一個(gè)程序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
出現(xiàn)幾個(gè)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
看來(lái)這本書(shū)的確太老了

$ ./a.out .
就會(huì)列出當(dāng)前目錄下的所有文件