<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > Linux下C應用程序開發(fā)

          Linux下C應用程序開發(fā)

          作者: 時間:2011-05-20 來源:網絡 收藏
          另外的 C 編程工具
            xxgdb
            xxgdb 是 gdb 的一個基于 X Window 系統(tǒng)的圖形界面. xxgdb 包括了命令行版的 gdb 上的所有特性. xxgdb 使你能通過按按鈕來執(zhí)行常用的命令. 設置了斷點的地方也用圖形來顯示.
          你能在一個 Xterm 窗口里鍵入下面的命令來運行它:
            xxgdb
            你能用 gdb 里任何有效的命令行選項來初始化 xxgdb . 此外 xxgdb 也有一些特有的命令行選項, 表 27.2 列出了這些選項.
            表 27.2. xxgdb 命令行選項.
            選 項 描 述
            db_name 指定所用調試器的名字, 缺省是 gdb.
            db_prompt 指定調試器提示符, 缺省為 gdb.
            gdbinit 指定初始化 gdb 的命令文件的文件名, 缺省為 .gdbinit.
            nx 告訴 xxgdb 不執(zhí)行 .gdbinit 文件.
            bigicon 使用大圖標.
            calls
            你可以在 sunsite.unc.edu FTP 站點用下面的路徑:
            /pub//devel/lang/c/calls.tar.Z
            來取得 calls , 一些舊版本的 CD-ROM 發(fā)行版里也附帶有. 因為它是一個有用的工具, 我們在這里也介紹一下. 如果你覺得有用的話, 從 BBS, FTP, 或另一張CD-ROM 上弄一個拷貝. calls 調用 GCC 的預處理器來處理給出的源程序文件, 然后輸出這些文件的里的函數調用樹圖.
            注意: 在你的系統(tǒng)上安裝 calls , 以超級用戶身份登錄后執(zhí)行下面的步驟: 1. 解壓和 untar   文件. 2. cd 進入 calls untar 后建立的子目錄. 3. 把名叫 calls 的文件移動到 /usr/bin 目錄. 4. 把名叫 calls.1 的文件移動到目錄 /usr/man/man1 . 5. 刪除 /tmp/calls 目錄. 這些步驟將把 calls 程序和它的指南頁安裝載你的系統(tǒng)上.
          ------------------------------------------------------------------------------
          --
            當 calls 打印出調用跟蹤結果時, 它在函數后面用中括號給出了函數所在文件的文件名:
          main [hello.c]
            如果函數并不是向 calls 給出的文件里的, calls 不知道所調用的函數來自哪里, 則只顯示函數的名字:
            printf
            calls 不對遞歸和靜態(tài)函數輸出. 遞歸函數顯示成下面的樣子:
            fact recursive in factorial.c >;>;>;
            靜態(tài)函數象這樣顯示:
            total [static in calculate.c]
            作為一個例子, 假設用 calls 處理下面的程序:
            #include stdio.h>;
            static void my_print (char *);
            static void my_print2 (char *);
            main ()
            {
            char my_string[] = hello world!;
            my_print (my_string);
            my_print2 (my_string);
            my_print (my_string);
            }
            void count_sum()
            {
            int i,sum=0;
            for(i=0; i1000000; i++)
            sum += i;
            }
            void my_print (char *string)
            {
            count_sum();
            (The string is %s , string);
            }
            void my_print2 (char *string)
            {
            char *string2;
            int size, i,sum =0;
            printf (The string printed backward is %s , string2);
            }
            將產生如下的輸出:
            1 __underflow [hello.c]
            2 main
            3 my_print [hello.c]
            4 count_sum [hello.c]
            5 printf
            6 my_print2 [hello.c]
            7 count_sum
            8 strlen
            9 malloc
            10 printf
            calls 有很多命令行選項來設置不同的輸出格式, 有關這些選項的更多信息請參考 calls 的指南頁. 方法是在命令行上鍵入 calls -h .
            calltree
            calltree與calls類似,初了輸出函數調用樹圖外,還有其它詳細的信息??梢詮膕unsite.unc.edu FTP 站點用下面的路徑
            :/pub//devel/lang/c/calltree.tar.gz得到calltree.
            cproto
            cproto 讀入 C 源程序文件并自動為每個函數產生原型申明. 用 cproto 可以在寫程序時為你節(jié)省大量用來定義函數原型的時間.
            如果你讓 cproto 處理下面的代碼(cproto hello.c):
            #include stdio.h>;
            static void my_print (char *);
            static void my_print2 (char *);
            main ()
            {
            char my_string[] = hello world!;
            my_print (my_string);
            my_print2 (my_string);
            }
            void my_print (char *string)
            {
            printf (The string is %s , string);
            }
            void my_print2 (char *string)
            {
            char *string2;
            int size, i;
            size = strlen (string);
            string2 = (char *) malloc (size + 1);
            for (i = 0; i size; i++)
            string2[size -1 - i] = string;
            string2[size] = '';
            printf (The string printed backward is %s , string2);
            }
            你將得到下面的輸出:
            /* hello.c */
            int main(void);
            int my_print(char *string);
            int my_print2(char *string);
            這個輸出可以重定向到一個定義函數原型的包含文件里.
            indent
            indent 實用程序是 Linux 里包含的另一個編程實用工具. 這個工具簡單的說就為你的代碼產生美觀的縮進的格式. indent 也有很多選項來指定如何格式化你的源代碼.這些選項的更多信息請看indent 的指南頁, 在命令行上鍵入 indent -h .
            下面的例子是 indent 的缺省輸出:
            運行 indent 以前的 C 代碼:
            #include stdio.h>;
            static void my_print (char *);
            static void my_print2 (char *);
            main ()
            {
            char my_string[] = hello world!;
            my_print (my_string);
            my_print2 (my_string);
            }
            void my_print (char *string)
            {
            printf (The string is %s , string);
            }
            void my_print2 (char *string)
            {
            char *string2; int size, i;
            size = strlen (string);
            string2 = (char *) malloc (size + 1);
            for (i = 0; i size; i++) string2[size -1 - i] = string;
            string2[size] = '';
            printf (The string printed backward is %s , string2);
            }
            運行 indent 后的 C 代碼:
            #include stdio.h>;
            static void my_print (char *);
            static void my_print2 (char *);
            main ()
            {
            char my_string[] = hello world!;
            my_print (my_string);
            my_print2 (my_string);
            }
            void
            my_print (char *string)
            {
            printf (The string is %s , string);
            }
            void
            my_print2 (char *string)
            {
            char *string2;
            int size, i;
            size = strlen (string);
            string2 = (char *) malloc (size + 1);
            for (i = 0; i size; i++)
            string2[size - 1 - i] = string;
            string2[size] = '';
            printf (The string printed backward is %s , string2);
            }
            indent 并不改變代碼的實質內容, 而只是改變代碼的外觀. 使它變得更可讀, 這永遠是一件好事.
            gprof
            gprof 是安裝在你的 Linux 系統(tǒng)的 /usr/bin 目錄下的一個程序. 它使你能剖析你的程序從而知道程序的哪一個部分在執(zhí)行時最費時間.
            gprof 將告訴你程序里每個函數被調用的次數和每個函數執(zhí)行時所占時間的百分比. 你如果想提高你的程序性能的話這些信息非常有用.
            為了在你的程序上使用 gprof, 你必須在編譯程序時加上 -pg 選項. 這將使程序在每次執(zhí)行時產生一個叫 gmon.out 的文件. gprof 用這個文件產生剖析信息.
            在你運行了你的程序并產生了 gmon.out 文件后你能用下面的命令獲得剖析信息:
            gprof program_name>;
            參數 program_name 是產生 gmon.out 文件的程序的名字.
            為了說明問題,在程序中增加了函數count_sum()以消耗CPU時間,程序如下
            #include stdio.h>;
            static void my_print (char *);
            static void my_print2 (char *);
            main ()
            {
            char my_string[] = hello world!;
            my_print (my_string);
            my_print2 (my_string);
            my_print (my_string);
            }
            void count_sum()
            {
            int i,sum=0;
            for(i=0; i1000000; i++)
            sum += i;
            }
            void my_print (char *string)
            {
            count_sum();
            printf (The string is %s , string);
            }
            void my_print2 (char *string)
            {
            char *string2;
            int size, i,sum =0;
            count_sum();
            size = strlen (string);
            string2 = (char *) malloc (size + 1);
            for (i = 0; i size; i++) string2[size -1 - i] = string;
            string2[size] = '';
            for(i=0; i5000000; i++)
            sum += i;
            printf (The string printed backward is %s , string2);
            }
            $ gcc -pg -o hello hello.c
            $ ./hello
            $ gprof hello | more
            將產生以下的輸出
            Flat profile:
            Each sample counts as 0.01 seconds.
            % cumulative self self total
            time seconds seconds calls us/call us/call name
            69.23 0.09 0.09 1 90000.00 103333.33 my_print2
            30.77 0.13 0.04 3 13333.33 13333.33 count_sum
            0.00 0.13 0.00 2 0.00 13333.33 my_print
            % 執(zhí)行此函數所占用的時間占程序總
            time 執(zhí)行時間的百分比
            cumulative 累計秒數 執(zhí)行此函數花費的時間
            seconds (包括此函數調用其它函數花費的時間)
            self 執(zhí)行此函數花費的時間
            seconds (調用其它函數花費的時間不計算在內)
            calls 調用次數
            self 每此執(zhí)行此函數花費的微秒時間
            us/call
            total 每此執(zhí)行此函數加上它調用其它函數
            us/call 花費的微秒時間
            name 函數名
            由以上數據可以看出,執(zhí)行my_print()函數本身沒花費什么時間,但是它又調用了
          count_sum()函數,所以累計秒數為0.13.
            技巧: gprof 產生的剖析數據很大, 如果你想檢查這些數據的話最好把輸出重定向到一個文件里. linux操作系統(tǒng)文章專題:linux操作系統(tǒng)詳解(linux不再難懂)

          linux相關文章:linux教程



          上一頁 1 2 3 下一頁

          評論


          相關推薦

          技術專區(qū)

          關閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();