之前在Windows下學習的時候,我一直使用Eclipse寫C語言程序,比較標準C,GTK+.後來忽有一天學習Linux的時候,看到軟件安裝那裏說了make這個工具,一時之間大是喜歡.由於我本人特別喜歡命令行的方式,因而我把系統換成了Ubuntu,安裝了gcc,g++,gdb,make等一系列工具,下載了網上流傳十分之廣的<跟我一塊兒寫Makefile>. shell
而後我如今寫程序,學校的做業我就是在Windows下用eclipse寫代碼,用UE寫makefile,在CMD中make編譯 app
若是是我本身學習的話,那絕對是Linux下寫,那編譯速度,那流暢,那感受,, eclipse
下面是一個很小的程序,就是在控制檯輸入兩個整數,而後輸出它們的和 函數
#include<stdio.h> #include"sum.h" int main( void ){ int a = 0; int b = 0; puts("輸入兩個整數:"); scanf("%d %d",&a,&b); printf("%d + %d = %d\n",a,b,sum(a,b)); return 0; }
#ifndef _SUM_H_ #define _SUM_H_ int sum( int a , int b ); #endif//_SUM_H_
#include"sum.h" int sum( int a , int b ){ return a + b; }
OBJS=test.o sum.o test:${OBJS} gcc -o test ${OBJS} test.o:test.c sum.h gcc -c -Wall test.c sum.o:sum.c sum.h gcc -c -Wall sum.c .PHONY:clean clean: -rm test ${OBJS}
因而個人運行結果是: 工具
不過再我看了<跟我一塊兒寫makefile> Makefile介紹中的隱晦規則後,個人makefile文件成了: 學習
OBJS=test.o sum.o test:${OBJS} test.o:test.c sum.h sum.o:sum.c sum.h .PHONY:clean clean: -rm test ${OBJS}
因而,個人編譯效果是: ui
在執行make時,不是我但願的gcc -c -Wall,而是cc -c,這顯示不是我想要的.因而我再查資料.終於在百度百科上查到了資料,因而makefile成了: spa
CC = gcc CCFLAGS = -c -Wall OBJS=test.o sum.o test:${OBJS} test.o:test.c sum.h sum.o:sum.c sum.h .PHONY:clean clean: -rm test ${OBJS}
運行效果: 命令行
其中cc命令是UNIX上的C編譯器,在Linux中cc和gcc就是一個東西: code
laolang@laolang-Lenovo-G470:~/code/makefile/genwo/one/five$ which cc /usr/bin/cc laolang@laolang-Lenovo-G470:~/code/makefile/genwo/one/five$ cd /usr/bin laolang@laolang-Lenovo-G470:/usr/bin$ ls -l cc lrwxrwxrwx 1 root root 20 10月 14 03:48 cc -> /etc/alternatives/cc laolang@laolang-Lenovo-G470:/usr/bin$ cd /etc/alternatives/ laolang@laolang-Lenovo-G470:/etc/alternatives$ ls -l cc lrwxrwxrwx 1 root root 12 10月 14 03:48 cc -> /usr/bin/gcc laolang@laolang-Lenovo-G470:/etc/alternatives$
在網上看到過可使用 make 實現所有從新編譯,看了僞目標,個人makefile就成了這樣:
CC=gcc CCFLAGS=-c -Wall OBJS=test.o sum.o test:${OBJS} test.o:test.c sum.h sum.o:sum.c sum.h .PHONY:clean-all clean-objs clean-app rebuild clean-all:clean-objs clean-app clean-objs: -rm ${OBJS} clean-app: -rm test rebuild: clean-all make
不過運行效果好像不太理想
laolang@laolang-Lenovo-G470:~/code/makefile/genwo/one/five$ make rebuild rm test.o sum.o rm test make make[1]: 正在進入目錄 `/home/laolang/code/makefile/genwo/one/five' gcc -c -o test.o test.c gcc -c -o sum.o sum.c gcc test.o sum.o -o test make[1]:正在離開目錄 `/home/laolang/code/makefile/genwo/one/five' laolang@laolang-Lenovo-G470:~/code/makefile/genwo/one/five$
在寫主目標的依賴時,要寫不少的.o,並且在寫主程序的命令時,要寫宏替換,因而在看了<Linux C 一站式學習中的>makefile相關部分後,個人Makefile 成了:
# 程序的名字 PROGRAM=test # 程序源代碼集 C_SOURCES=test.c sum.c # .o文件 # ${C_SOURCES:.c=.o} 是一個變量替換語法 # 把全部的.c替換爲.o C_OBJS=${C_SOURCES:.c=.o} # CC 指定 gcc 爲編譯器 # CCFLAGS 指定編譯選項 CC=gcc CCFLAGS=-c -Wall # 默認目標 all:${PROGRAM} # 建立主程序 ${PROGRAM}:${C_OBJS} ${CC} -o $@ ${C_OBJS} # 生成各.o文件 test.o:test.c sum.h sum.o:sum.c sum.h .PHNOY:clean run rebuild # 清除文件 clean: -rm ${C_OBJS} ${PROGRAM} # 運行程序 run: @echo "運行程序\n" ./${PROGRAM} # 從新構建 rebuild: @echo "從新構建\n" make clean && make
makefile的基本語法:
目標:依賴
命令
現階段基本上上也就是使用自動推導依賴,從而不用寫那些gcc -c -Wall test.c 或者 gcc test.o -o test,再有就是使用僞目標實現從新編譯和清除目標文件和可可執行文件
而後仍是有問題
1. 使用@echo進行輸出時,老是會出問題,我又不想寫太多的gcc命令
2.自動化變量和多個makefile的使用
3.條件判斷