makefile這玩意在上學時就應該學,可是一直沉浸於ide的**,所謂「死於安樂」,直到現在一把年紀才開始接觸這種基礎東西。
建立c程式
先寫個c程式,儲存在main.c裡:
view plain
// // file: main.c
// #include
int main()
看看我這時的目錄結構
view plain
~/code/makefile$ ls
main.c
這時敲個「make」命令試試?
view plain
~/code/makefile$ make
make: *** 沒有指明目標並且找不到 makefile。 停止。
建立makefile檔案
顯然,我們要建個名為「makefile」的檔案。先建乙個空的名為makefile的檔案:
view plain
~/code/makefile$ touch makefile
tommy@tommy-zheng-thinkpad-t61:~/code/makefile$ ls
main.c makefile
這時再試試「make」命令:
view plain
~/code/makefile$ make
make: *** 無目標。 停止。
加入target
錯誤「無目標」告訴我們需要在makefile裡新增一些東西:
view plain
# 注釋 file: makefile
target:
再執行「make」命令:
view plain
~/code/makefile$ make
make: 沒有什麼可以做的為 `target'。
可以看到前面「無目標」的錯誤已經解決了。
加入命令
繼續往makefile裡新增東西:
view plain
# 注釋 file: makefile
target:
gcc -o tommy main.c # 注意,最前面是tab,不是空格!
好了,執行make命令:
view plain
~/code/makefile$ make
gcc -o tommy main.c
~/code/makefile$ ls
main.c makefile tommy
main.c被編譯了,乙個可執行檔案「tommy」產生了。我們執行一下試試:
view plain
~/code/makefile$ ./tommy
tommy: 5
我們的makefile寫完了!!!
好吧,之前的makefile實在是太簡單,以至於沒什麼實際的用途。現在再深入研究下。
target
在前面的makefile裡,有乙個叫"target"的東西。其實它可以是任何名字,而且乙個makefile裡可以有多個target。比如下面的makefile:
view plain
# 注釋 file: makefile
tommy:
gcc -o tommy main.c # 注意,最前面是tab,不是空格!
dosomething:
echo just for fun.
給make命令乙個引數:
view plain
~/code/makefile$ make dosomething
echo just for fun.
just for fun.
可以看到,make可以用來執行任何乙個target底下的命令,而這種命令並不侷限於gcc這種編譯的命令。每個target用冒號隔開。如果make命令沒有指定哪個target,那第乙個target下的命令會被執行。
dependencies
make命令一次只能處理乙個target,但如果我想一次處理多個target怎麼辦?這時可以為乙個target在冒號後面指定它所依賴的target。修改下makefile:
view plain
# 注釋 file: makefile
tommy:
gcc -o tommy main.c # 注意,最前面是tab,不是空格!
dosomething: dofirst dosecond # 先執行另兩個target的命令
echo just for fun.
dofirst:
echo first.
dosecond:
echo second.
donothing:
echo nothing.
make一下看看:
view plain
~/code/makefile$ make dosomething
echo first.
first.
echo second.
second.
echo just for fun.
just for fun.
可以看到,dofirst和dosecond在dosomething之前都被make了,但tommy和donothing都沒有執行。
編譯多個c檔案
現在增加兩個檔案f.h和f.c,同時改一下main.c:
view plain
// file: f.h
int add(int, int);
// file: f.c
int add(int a, int b)
// // file: main.c
// #include
#include "f.h"
int main()
看看我的目錄結構:
view plain
~/code/makefile$ ls
f.c f.h main.c makefile
基於前面的「dependencies」得到的結論,我們可以改寫makefile,來讓main函式呼叫f函式:
view plain
# file: makefile
tommy: main.o f.o
gcc -o tommy main.o f.o
main.o:
gcc -c main.c -o main.o
f.o:
gcc -c f.c -o f.o
看下生成的結果:
view plain
~/code/makefile$ make
gcc -c main.c -o main.o
gcc -c f.c -o f.o
gcc -o tommy main.o f.o
~/code/makefile$ ls
f.c f.h f.o main.c main.o makefile tommy
~/code/makefile$ ./tommy
tommy-add: 6
可以看到我們的生成了我們想要的東西。
clean與install
經常可以看到「make clean」和「make install」的命令。我們也可以提供它們:
view plain
# file: makefile
tommy: main.o f.o
gcc -o tommy main.o f.o
main.o:
gcc -c main.c -o main.o
f.o:
gcc -c f.c -o f.o
clean:
rm *.o
install:
mv tommy /usr/local
巨集最後再研究下makefile裡的巨集。其實就是定義乙個變數,之後再使用它:
view plain
# file: makefile
install_path = /usr/local
temp_files = *.o
tommy: main.o f.o
gcc -o tommy \
main.o f.o # 這裡演示反斜槓用於換行,注意反斜槓後沒有空格,行首是tab而非空格
main.o:
gcc -c main.c -o main.o
f.o:
gcc -c f.c -o f.o
clean:
rm $(temp_files)
install:
mv tommy $(install_path)
(完)原文:
Redis從無到有
redis最佳執行環境是linux作業系統,所以要在虛擬機器裡的linux系統裡安裝redis 安裝教程 安裝過程中遇到的問題 1 windosws系統預設是沒開啟虛擬機器功能的,進入bios設定,將相應的disable改為enable 2 license not accept 依次輸入 1,2,c...
GitHub從無到有
2018年07月04日 09 23 40 夏雨薇安 首先你得註冊乙個自己的github賬號,註冊 有了自己的賬號以後,就可以進行登入,開始建立乙個新的專案 建立乙個新的專案,填寫專案名稱,描述 建立完成之後,跳轉到下面的頁面,下面紅框中的 要記住,在後面上傳 的時候需要使用 出現以下介面 第一步 c...
docker學習,從無到有
安裝 最好選用centos7 檢查核心版本 uname r 網上很多教程說centos6.5必須要公升級核心到3.10才能使用docker,其實是 可選 公升級,但最好公升級。安裝docker最新版 yum y install docker ce 檢視docker版本 docker version ...