## GCC(GNU 編譯器套件) - WIKI > GNU編譯器套裝(英語:GNU Compiler Collection,縮寫為GCC)是GNU計劃製作的一種最佳化編譯器,支援各種程式語言、作業系統、電腦系統結構。該編譯器是以GPL及LGPL授權條款所發行的自由軟體,也是GNU計劃的關鍵部分,還是GNU工具鏈的主要組成部份之一。GCC(特別是其中的C語言編譯器)也常被認為是跨平台編譯器的事實標準。1985年由理察·馬修·斯托曼開始發展,現在由自由軟體基金會負責維護工作。截至2019年,GCC大約有1500萬行代碼,是現存最大的自由程式之一。[4] 它在自由軟體的發展中發揮了重要作用,不僅是一個工具,還是一個典例。 > > 原名為GNU C語言編譯器(GNU C Compiler),因為它原本只能處理C語言。同年12月,新的GCC編譯器可以編譯`C++`語言。後來又為Fortran、Pascal、Objective-C、Java、Ada,Go等其他語言開發了前端。C和`C++`編譯器也支援OpenMP和OpenACC規範。 > > GCC編譯器已經被移植到比其他編譯器更多的平台和指令集架構上,並被廣泛部署在開發自由和專有軟體的工具中。GCC還可用於許多嵌入式系統,包括基於ARM和Power ISA的晶片。 > > GCC不僅是GNU作業系統的官方編譯器,還是許多類UNIX系統和Linux發行版的標準編譯器。BSD家族中的大部分作業系統也在GCC釋出之後轉用GCC;不過FreeBSD、OpenBSD和Apple macOS已經轉向了Clang編譯器[5],主要是因為授權問題。[6][7][8]GCC也可以編譯Windows、Android、iOS、Solaris、HP-UX、IBM AIX和DOS系統的代碼。GCC原本用C開發,後來因為LLVM、Clang的崛起,它更快地將開發語言轉換為`C++`。許多`C`的愛好者在對`C++`一知半解的情況下主觀認定`C++`的效能一定會輸給`C`,但是Ian Lance Taylor給出了不同的意見,並表明`C++`不但效能不輸給`C`,而且能設計出更好,更容易維護的程式[9][10]。 ## GCC編譯過程 GCC Compilation Process 1.7 GCC Compilation Process ![image](https://hackmd.io/_uploads/SkIHr3wKp.png =500x) - [color=#ddb461] [GCC and Make - Compiling, Linking and Building C/`C++` Applications](https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html) ![image](https://hackmd.io/_uploads/HJtfP2PFa.png) ### Compiling, Linking and Building ![image](https://hackmd.io/_uploads/SkyKDnPY6.png =500x) [Embedded Systems in Pyjama。Why I Don't Write Makefiles Anymore!](https://inpyjama.com/beyond-make-why-i-dont-write-makefiles/) ### 4 Stages of Compilation Process ![image](https://hackmd.io/_uploads/SJgO1aPYT.png =500x) [ITSC 3181 Introduction to Computer Architecture。Chapter 2.12: Compilation, Assembling, Linking and Program Execution](chrome-extension://mhnlakgilnojmhinhkckjpncpbhabphi/pages/pdf/web/viewer.html?file=https%3A%2F%2Fpasslab.github.io%2FITSC3181%2Fnotes%2Flecture02_CompilationAssemblingLinkingProgramExecution.pdf) 編譯(Compiling)、連結(Linking)和建構(Building)是將源碼轉換成可執行檔案的三個主要階段。 ### Compiling 編譯是將源碼(如 `.c` 文件)轉換成中間代碼(通常是組合語言或機器碼)的過程。這個過程涉及源碼的解析、語法分析、語義分析以及代碼優化。GCC在這一階段會執行多種優化,以提升程式的性能和效率。 ### Linking 連結是將編譯後產生的一個或多個目標文件(`.o` 或 `.obj` 文件)與必要的庫文件合併,生成單一可執行檔案的過程。在這個階段,解析器(Linker)會解析所有目標文件之間的依賴關係,並把它們結合在一起,處理如符號解析和重定位等任務。 ### Building 建構是編譯和連結的總稱,通常指的是整個從源碼到可執行檔案的轉換過程。這包括預處理、編譯、組譯、連結和打包等步驟。在這個過程中,可以使用像Make這樣的自動化建構工具,透過Makefile來定義建構規則和指令,自動化執行編譯和連結的命令,從而提高開發效率。 - Tools and Steps for Program Execution ![image](https://hackmd.io/_uploads/HJlZepvY6.png =600x) - Code Can be in Assembly Language ![image](https://hackmd.io/_uploads/SJDMgawKT.png =600x) ![image](https://hackmd.io/_uploads/H1ryv3wta.png =500x) - [How to Build a CMake-Based Project](https://preshing.com/20170511/how-to-build-a-cmake-based-project/) ## `make`與 "Makefile" ## 參考資料 ### 編譯 ### [2023.02。sysprog。你所不知道的 C 語言」系列講座](https://hackmd.io/@sysprog/c-prog/%2F%40sysprog%2Fc-programming) #### [2021.03。The Cloudflare Blog。How to execute an object file: Part 1](https://blog.cloudflare.com/how-to-execute-an-object-file-part-1) ![image](https://hackmd.io/_uploads/rkbFqRptT.png =400x) ![image](https://hackmd.io/_uploads/SyCtcApF6.png =300x) #### [2015。How A Compiler Works: GNU Toolchain](https://www.slideshare.net/jserv/how-a-compiler-works-gnu-toolchain) ![image](https://hackmd.io/_uploads/SkK27mAY6.png =400x) ![image](https://hackmd.io/_uploads/B1gNBm0Yp.png =400x) ![image](https://hackmd.io/_uploads/BJayUQ0tp.png =400x) - GCC Structure ![image](https://hackmd.io/_uploads/S1hlDV0KT.png =400x) ### Make #### 投影片 [第二章 Linux C编程基础--make](https://wenku.baidu.com/view/416e3f0716fc700abb68fc5f?aggId=aadf5f4fa6e9856a561252d380eb6294dc882219&fr=catalogMain_text_ernie_recall_backup_new%3Awk_recommend_main2&_wkts_=1705655182209&bdQuery=make+%E4%BA%A4%E5%8F%89%E7%BC%96%E8%AF%91&needWelcomeRecommand=1) #### [2004。陳浩。跟我一起寫Makefile](https://haoel.blog.csdn.net/article/details/2886?ydreferer=aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3hjeGh6amwvYXJ0aWNsZS9kZXRhaWxzLzEyODY3MDg0Mw%3D%3D) 經典文章 #### [2019。程序员良许。一文入门Makefile](https://zhuanlan.zhihu.com/p/56489231) ![image](https://hackmd.io/_uploads/BkGWu2DKp.png =400x) #### [2002。Luis Fernandes。make and Makefiles](https://www.ecb.torontomu.ca/~elf/ele428/makefile-tutorial.html) ![image](https://hackmd.io/_uploads/SJbD610Fa.png) > Figure 1.Annotated Makefile > - **<span style="color:blue;">Comments begin with a #.</span>** > - **<span style="color:red;">Targets indicate what the Makefile is capable of.</span>** > - **<span style="color:lightgreen;">Dependancies indicate what each target depends on.</span>** > - **<span style="color:darkgreen;">Commands indicate how the targets are to be built.</span>** ![image](https://hackmd.io/_uploads/SJ1RT1CYp.png) ![image](https://hackmd.io/_uploads/BybeRJRtp.png) #### [2024.01。ysprog。Makefile 語法和示範](https://hackmd.io/@sysprog/SySTMXPvl) #### [Makefile 简明教程](https://www.zhaixue.cc/makefile/makefile-intro.html) ![image](https://hackmd.io/_uploads/HJhL0JCKa.png =300x) * cpp: preprocessor * ccl: compiler * as: assembler * ld: linker * ar: static library authoring tool #### [ Lex & Yacc 學習筆記系列 第 13 篇[Day13] Makefile 介紹](https://ithelp.ithome.com.tw/articles/10317723) #### [寫 Web 也可以用 Makefile:好好管理你的環境流程](https://www.blogger.com/profile/11621593992969041904)