posts - 403, comments - 310, trackbacks - 0, articles - 7
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          編譯《UNIX環境高級編程》中的第一個程序

          Posted on 2007-08-03 01:13 ZelluX 閱讀(2235) 評論(2)  編輯  收藏 所屬分類: C/C++
          如果直接編譯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為例
          #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);
          }

          $ 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 .
          就會列出當前目錄下的所有文件


          評論

          # re: 編譯《UNIX環境高級編程》中的第一個程序  回復  更多評論   

          2008-02-20 22:25 by donnie0219
          哥們兒,我也一樣.

          # re: 編譯《UNIX環境高級編程》中的第一個程序  回復  更多評論   

          2008-12-21 22:22 by fanzhiyong
          http://www.apuebook.com/ 下載源碼,并按照readme作,就沒有你說的問題
          主站蜘蛛池模板: 方城县| 珲春市| 云阳县| 怀柔区| 天镇县| 永城市| 新营市| 剑阁县| 忻州市| 贵定县| 天镇县| 邓州市| 永仁县| 贵南县| 长阳| 深州市| 台州市| 葫芦岛市| 中牟县| 三明市| 象山县| 仙桃市| 富锦市| 大安市| 华坪县| 自贡市| 普陀区| 同德县| 博罗县| 如东县| 绵阳市| 晋江市| 曲沃县| 阳信县| 通渭县| 突泉县| 大足县| 民和| 潢川县| 弥勒县| 曲周县|