Creative Commons
Protesters Demonstrate to Block the Release of 'The Cove' in Japan

about makefile with vim

roselone posted @ 2009年9月16日 07:09 in 文摘 , 1273 阅读

現在小弟來說Makefile檔:
我們先建立一個簡單的程式: 名為 hello.c
如何寫一個最簡單的Makefile檔來編譯它?
# vim Makefile
進入編輯畫面:

# it is a test
all:hello.c
    gcc hello.c -o hello
clean:
    rm -f hello

存檔離開...當我們執行 # make, 就會把名為hello 的執行檔編譯出來
當我們輸入 # make clean, 就會把名為hello 的執行檔給刪除
而Makefile內的#符號指的是註解
ps: 注意: gcc hello.c -o hello 前的空白, 請使用tab鍵,  千萬別使用space鍵喔!!!!

再來複雜一點的:
我們再分別建立a.h, b.h, c.h, main.c, 2.c, 3.c 六個檔案
我們試著編譯它們
# vim Makefile1

mytest:main.o 2.o 3.o
    gcc -o mytest main.o 2.o 3.o
main.o:main.c a.h
    gcc -c main.c
2.o: 2.c a.h b.h
    gcc -c 2.c
3.o:3.c b.h c.h
    gcc -c 3.c

不用擔心, 很容易理解:
當 你打上 # make  -f Makefile1執行後, make就會去找第一行的必要條件: main.o 2.o 3.o, 當在目錄裡卻沒這三個檔, 所以make會在Makefile1往下找main.o/2.o/3.o分別把這三個檔案給編出來, 然後再把mytest這個執行檔給編譯出來, -f 是什麼? 第一次我們已建立一個名Makefile檔, 現在我們又建立一個Makefile1檔, 所以-f是告訴make去執行你所指定的Makefile檔...

再來把上面的變德更複雜一點: 加入變數/if判斷式/install
all: mytest
CC = gcc
INSTDIR = /usr/local/bin
INCLUDE = .
CFLAGS = -g -Wall -ansi

mytest: main.o 2.o 3.o
    $(CC) -o mytest main.o 2.o 3.o
main.o: main.c a.h
    $(CC) -I$(INCLUDE) $(CFLAGS) -c main.c
2.o: 2.c a.h b.h
    $(CC) -I$(INCLUDE) $(CFLAGS) -c 2.c
3.o: 3.c b.h c.h
    $(CC) -I$(INCLUDE) $(CFLAGS) -c 3.c

clean:
    rm main.o 2.o 3.o

install: mytest
    @if[   -d $(INSTDIR)   ]; \
          then   \
          cp mytest $(INSTDIR);\
          chmod a+x $$(INSTDIR)/mytest; \
          chmod og-w $(INSTDIR)/mytest; \
          echo "Installed in $(INSTDIR)";\
    else \
          echo "Sorry, $(INSTDIR) does not exist";\
    fi

哇, 有點多, 不過也一樣很容易理解:
all: mytest 為必要條件, 目錄裡沒有, 就會往下搜mytest: main.o 2.o 3.o
其它的上面已提過

CC = gcc
INSTDIR = /usr/local/bin
INCLUDE = .
CFLAGS = -g -Wall -ansi

此乃變數宣告, 使用變數方式: $(var)
INCLUDE=. 意指目前所在目錄之意

那這些個 -g  -I -Wall是什麼東東?
-ansi : 程式要求依據ansi c標準
-I : 追加include檔案的搜尋路徑 
-Wall : 編譯時顯示所有的警告訊息
-g : 編入除錯資訊(要使用GDB除錯一定要加)

install: 裡面全是shell程式的部份, 未來有空, 小弟會在此詳解
我先說個大概:
@if[   -d $(INSTDIR)   ];
@ 指不要讓make install在執行時, 不要印出執行的細節
另外切記-d前面的空白處及後面R)    ]空白處, 一樣一定要使用tab鍵, 不要使用space
-d 是要make判斷是該檔案是否為目錄且是存在的
cp, 不用說就是copy
chmod a+x 改變mytest 的權限屬性, 意指owner, group, other三個身份都可以執行這個檔案
chmod og-w 改變mytest 的權限屬性, 意指group, other三個身份都不可以編寫修改這個檔案
echo ".." 印出字串
fi 相當於endif的意思

housekeeping service 说:
2021年9月20日 18:15

Maids are authorities. Though they usually are portrayed diversely in shows and with television, him or her expect in to the future into the house, handle this cleaning, in addition to leave learning they've fascinated and maintained you delighted. If you take the time to hire the suitable professionals with the job, you will be far happier while using the results in comparison with you thought you'll probably be. What steps if you ever take before securing a person to accomplish these tasks available for you.

jnanabhumiap.in 说:
2024年1月08日 23:55

JNANABHUMI AP provides a CBSE syllabus for all classes for the academic year 2024 has been designed as per the guidelines of the CBSE Board. The syllabus offers a conceptual background and lays the groundwork for the Class 10 Board exams. jnanabhumiap.in By visiting the page, students will find the downloadable pdf of the reduced CBSE 10th Syllabus along with the deleted portion of the syllabus for each subject. So, students are advised to prepare for the exam, as per the syllabus mentioned here.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter