GCC & Makefile === ###### tags: `Code` gcc -o myexe main.c - **傳統的編譯:** - gcc foo1.c -o foo1 - 事實上,上面的這個編譯方式可以拆解成: - gcc foo1.c -c - gcc foo1.o -o foo1 - 編譯的過程是將原始碼(foo1.c)先利用-c參數編譯成.o(object file),然後再鏈結函式庫成為一個binary。-c即compile之意。 - gcc foo1.c $SACLIB/sacio.a -O3 -g -Wall -ansi -o foo1 | 編譯的參數 | 說明 | | -------- | --- | | -c | 編譯但不進行鏈結,會產生一個跟原始碼相同名稱但副檔名為.o的目的檔。 | | -O | 表示最佳化的程度。-O預設就是-O1,你可以指定成-O2或-O3,數字越大表示最佳化程度越好,但是會增加編譯時間。 | | -g | 把偵錯資訊也編譯進去。當你有需要使用GDB軟體進行偵錯,必須加入-g使GDB能夠讀取。一般情況下可以不用-g,因為他也會增加binary大小。 | | -Wall | 顯示警告訊息。使用這個參數會在編譯時顯示更多的警告訊息。這個參數相當有用,特別是找不到libs/headers之類的問題。 | | -ansi | 使用相容ANSI標準的編譯方式。ANSI是American National Standards Institute,即美國國家標準學會的簡稱。-ansi可以增加程式的可移植性。 | | $SACLIB | 其中的$SACLIB就是一個變數名稱,她必須被指定正確的值。執行這個命令前必須先確定這個變數是有被指派正確的值才行。.a檔是一個靜態函式(static library),關於靜態跟共享的觀念稍候解釋。 | ***- Sample:*** ```makefile CC = gcc INC = -I. OLIST = Pointer_main.o Pointer_Test: clean $(OLIST) $(CC) $(INC) $(OLIST) -o ./bin/$@ Pointer_main.o: $(CC) $(INC) ./Pointer_main.c -c clean: rm -rf *.o ``` - For example, consider the following declaration: https://bit.ly/2OouzgJ ```makefile all: library.cpp main.cpp $@ evaluates to all $< evaluates to library.cpp $^ evaluates to library.cpp main.cpp ``` - Using patterns and special variables https://bit.ly/2NeO3bo ```makefile When wildcard % appears in the dependency list, it is replaced with the same string that was used to perform substitution in the target. Inside actions we can use: $@ to represent the full target name of the current target $? returns the dependencies that are newer than the current target $* returns the text that corresponds to % in the target $< returns the name of the first dependency $^ returns the names of all the dependencies with space as the delimiter ``` Reference === [Makefile範例教學](https://goo.gl/69J2ad) [使用 gcc 自製 C/C++ 靜態、共享與動態載入函式庫教學](https://goo.gl/qxVXwZ) [Makefile 語法簡介](https://goo.gl/49p816) [陳鍾誠_gcc 編譯器](https://goo.gl/n7Dz8H) [Makefile 語法和示範](https://goo.gl/KgS9PG) [線上coding](http://ideone.com/) [Makefile的寫法教學 video](https://bit.ly/2xe9X4i)