GCC編譯簡單範例

來源:互聯網
上載者:User
1.單一程式:列印Hello

源檔案為hello.c

無選項編譯連結

gcc hello.c

將hello.c預先處理、彙編、編譯並連結形成可執行檔。這裡未指定輸出檔案,預設輸出為a.out

選項 -o

-o選項用來指定輸出檔案的檔案名稱

gcc hello.c -o hello

選項 -E

gcc -E hello.c -o hello.i

將hello.c預先處理輸出hello.i檔案

參數 -S

gcc -S hello.i

將預先處理輸出檔案hello.i彙編成hello.s檔案

參數 -c

gcc -c hello.s

將彙編輸出檔案hello.s編譯輸出hello.o檔案

無選項連結

gcc hello.o -o hello

參數 -O

gcc -O1 hello.c -o hello

使用編譯最佳化層級1編譯器.層級為1-3,層級越大最佳化效果越好,但編譯時間越長.


2.主程式子程式連結:子程式的編譯

假設我們在一個主程式裡有調用了另一個子程式,例如以thanks.c這個主程式去調用thanks_2.c這個程式

多個檔案一起編譯

gcc thanks.c thanks_2.c -o thanks

將thanks.c和thanks_2.c分別編譯後連結成thanks可執行檔

分別編譯各個源檔案,之後對編譯後輸出的目標檔案連結

gcc -c thanks.cgcc -c thanks_2.cgcc -o thanks.o thanks_2.o -o test

以上兩種方法相比較,第一中方法編譯時間需要所有檔案重新編譯,而第二種方法可以只重新編譯修改的檔案,未修改的檔案不用重新編譯。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.