Wednesday, September 14, 2011

撰寫Makefile教學

這裡我們討論當你在Unix like的系統之中如何透過GNU tool幫你建立你整個專案,也許你的程式只有幾個檔案,慢慢用手編是簡單也沒問題,但是如果是大型的專案,超過幾十個檔案而且需要連結不少的函式庫,那你應該怎麼做呢?最方便的就是學習makefile和make指令,使用這個工具幫你做編譯和連結的動作。

※ 使用make好處

* 透過你所設定的條件幫你編譯好
* 方便專案管理
* 會透過檔案比對,依照相依性來編譯,不會全都編浪費時間
* 可以同時編譯函式庫或是檔案

※ make常用指令

* make -k: 會讓make在遇到錯誤的時候仍然運行,而不會停在第一個問題
* make -n: 只印出將會進行的工作,而不會真的去執行
* make -f makefile_name: 告訴make需要用那個makefile檔案。當你的make檔不是叫makefile的時候,需要自行透過-f加上你檔案名字,make才找的到你的makefile

※ make指令格式

make [option] [target] option就是上面的設定項目,而target等等會講到,就是我們將要產生出來的目標,可以接很多個目標,如果目標不寫的話預設是all。

Example:

make -n all clean
make install
make
make -f makefile2 install

※ 撰寫makefile檔案

makefile是由一堆「目標」和其「相依性檔案」還有「法則」所組成的,而法則在寫的時候前面不可以使用空格,只能使用Tab鍵,而且同一法則要換行的話需要使用'\'字元,而要加入註解的話要用'#'為開頭字元。

* [target] 目標 - 產生出來的東西或某個項目
* [dependency] 相依性項目 - 目標受相依檔案改變需要重新產生目標
* [rule] 法則 - 如何讓相依性項目編譯和連結來產生目標

Example:

#這是makefile的格式(註解)
[target]: [dependency] [dependency]
[TAB][rule]
[TAB][rule]
[target]: [dependency]
[TAB][rule]

整個makefile就是利用上面的格式,目標和相依性項目還有法則,組合之後就可以編出一個執行檔了,我們下面就來舉個最容易的範例。

Example:

all: myapp app.doc
myapp: main.o a.o b.o
[tab]gcc main.o a.o b.o -o myapp
main.o: main.c a.h
[tab]gcc -c main.c
a.o: a.c a.h
[tab]gcc -c a.c
b.o: b.c b.h
[tab]gcc -c b.c

make (呼叫make執行)

這是個最簡單的範例,我們來看一下它怎麼運做的,首先執行make的時候都會執行all這個目標,如果沒有設定all這個目標,它就會拿第一個目標來產生,在這邊看到要產生all的話,需要兩個檔案myapp和app.doc(主程式和說明檔),make開始會去找尋如何產生myapp和 app.doc的方法,所以myapp會成為下一個要產生出來的目標,所以要產生出myapp的話需要有main.o, a.o, b.o這三個檔連結在一起才可以產生myapp執行檔,那該怎麼產生呢?我們的法則有寫要使用gcc main.o a.o b.o -o myapp來產生,但是我們連main.o, a.o, b.o都沒有了,怎麼可能可以產生myapp呢?所以現在要先產生出這些目的檔,以main.o來看,它就成為下一個目標,它需要main.c和a.h才可以產生出來,而產生的法則是gcc -c main.c就可以產生出main.o檔了而a.o, b.o也是同樣的方式產生,一旦main.o a.o b.o都產生了,那麼目標myapp就可以透過法則來產生出來了。

所以寫makefile最重要的事情就是先搞清楚你最後產生什麼東西,該怎麼產生,那它需要什麼檔,再依它需要的檔怎麼產生,寫它的法則,一直追到最後的結果就是怎麼產生.c檔,就是本來的source了,那就要自己寫了^^。

※ 多重target(目標)

目標就是我們所定最後要產生的結果,所以我們可以定一些目標,讓shell script幫我們做很多事情,而常用makefile裡面會寫的目標如。

* install 產生完可執行檔之後怎麼將程式安裝到指定位置
* clean 只想產生執行檔,剩下的目的檔不需要可以刪除
* uninstall 從系統之中整個移除你安裝的所有檔案

Example:

all: myapp app.doc
myapp: main.o a.o b.o
[tab]gcc main.o a.o b.o -o myapp
main.o: main.c a.h
[tab]gcc -c main.c
a.o: a.c a.h
[tab]gcc -c a.c
b.o: b.c b.h
[tab]gcc -c b.c
#install 安裝套件
install: myapp app.doc
[tab]cp myapp app.doc /usr/local/myapp/
#clean 刪除產生出來的目的檔
clean:
[tab]rm -f *.o

make install (呼叫make執行install target)

在範例中我們增加了install和clean兩個目標,install主要就是把產生出來的檔案複製到我們想要安裝的地方,而clean的話則是把目標下面有的.o檔都移除或是要重新建立專案的時候,先把所有的檔都移除。

※ make的巨集(macro)

想想如果一個專案檔案好幾十個的話,那你不就目標相依性還有法則一行一行打到死嗎?所以make可以使用巨集讓你很方便的更改你的資料,或是編譯器或是參數等等的使用方法。

Example:

CC = gcc 指定
$(CC) 叫用
CFLAGS = -ansi -Wall -g 指定
$(CFLAGS) 叫用

還可以使用一種將副檔名改成別的副檔名的巨集使用方式,可以讓你輕鬆的將本來的副檔名改成另一個副檔名,且指定給某巨集。

Example:

SRC = a.c b.c
OBJ = $(SRC:.c=.o)

有幾個特別的內部巨集,讓makeifle 更加簡明,每個巨集都是在使用之前才被展開,所以巨集的意義隨makefile的處理而有所不同。

* $? 代表需要重建的相依性項目
* $@ 目前的目標項目名稱
* $< 代表第一個相依性項目 * $* 代表第一個相依性項目,不過不含副檔名 還有兩個有用的特別字元,可以加在要執行的命令之前。 * - make會忽略命令的錯誤。 * @ make不會在標準輸出上,顯示要執行的命令。 Example: #compiler CC = gcc #cflags CFLAGS = -Wall -ansi -g #object OBJS = main.o a.o b.o #install path INSTALL_PATH = /usr/local/myapp/ all: myapp app.doc myapp: $(OBJS) [tab]$(CC) $(OBJS) -o $@ main.o: main.c a.h [tab]$(CC) $(CFLAGS) -c -o $@ $< a.o: a.c a.h [tab]$(CC) $(CFLAGS) -c -o $@ $< b.o: b.c b.h [tab]$(CC) $(CFLAGS) -c -o $@ $< #install 安裝套件 install: myapp app.doc [tab]cp myapp app.doc $(INSTALL_PATH) #clean 刪除產生出來的目的檔 clean: [tab]rm -f *.o make install (呼叫make執行install target) 這樣是不是方便多了,使用了$@ $<可以讓你少打很多字,而且把目的檔使用一個巨集表示,可以讓你方便管理,不會忘了打某個目的檔讓你一直編不出來而且在編譯的時候在CFLAGS可以設定編出來的模式為那一種,方便加入編譯的參數。 ※ make內建的法則 到目前為止我們都很成功的寫出makefile可以幫我們編譯和連結出執行檔,但是其實我們還是重覆打了很多很類似的指令,浪費了不少的時間,然而make提供我們很多內建的法則。 Example: #compiler CC = gcc #cflags CFLAGS = -Wall -ansi -g #object OBJS = main.o a.o b.o all: myapp app.doc myapp: $(OBJS) [tab]$(CC) $(OBJS) -o $@ main.o: main.c a.h a.o: a.c a.h b.o: b.c b.h 而make已經幫我們做了gcc -Wall -ansi -g -c -o main.o main.c(a.o b.o也一樣)的工作了為了什麼自動加入-Wall -ansi -g呢?因為我們巨集有設定CFLAGS這是make內件就有的巨集名稱,所以一旦設好了,使用內建法則的時候,就會自動的加入你要的編譯參數。 ※ make檔尾的法則 使用檔尾的延伸檔名估為一個法則,所以當一個檔案有吻合檔尾法則的時候,make就知道要使用什麼法則去產生目標。 Example: 格式 .[old_suffix].[new_suffix]: .c.o: [tab]$(CC) $(CFLAGS) -c -o $@ $< .cpp.o: [tab]g++ -c $< 如此就會把這目錄下面所有的.c檔變成.o檔,而法則就是去編譯它,而如果你想更懶一點的話還可以完全不寫,直接使用內建的法則,這樣也可以直接把目錄下面的所有檔都編好,為什麼呢?因為你要編出myapp的時候需要使用到$(OBJS)所以,就算你不寫.c.o或是任何的法則,make預設都會自己產生.o檔讓你可以連結出主程式。 Example: #compiler CC = gcc #cflags CFLAGS = -Wall -ansi -s #object OBJS = main.o a.o b.o all: myapp app.doc myapp: $(OBJS) [tab]$(CC) $(OBJS) -o $@ 那幹麻還要寫什麼檔尾法則呢?其實使用預設的法則有一些缺點,就是資料夾或是檔案相依性的問題,這樣預設法則根本就會有問題,因為你要 include的資料根本不在本目錄下面,所以這時候檔尾法則就很好用了,因為可以告訴make,針對所有的.c要變成.o都要以下面的rule來做。 Example: #compiler CC = gcc #cflags CFLAGS = -Wall -ansi -s #object OBJS = main.o a.o b.o #include path INCLUDE_PATH = include all: myapp.exe app.doc myapp.exe: $(OBJS) [tab]$(CC) $(OBJS) -o $@ .c.o: [tab]$(CC) -I$(INCLUDE_PATH) $(CFLAGS) -c -o $@ $< 上例使用檔尾法則告訴make我在編譯的時候,要用到的.h檔位置在include裡面,要去裡面找,這樣程式就不會說找不到標頭檔的問題了,但是如果你使用內件的法則,它只會選擇目前目錄下面的檔案而已。 此外在更新的版本裡還有另一種語法,就是利用萬用字元的語法,它不限用在檔尾,還可以用在全部檔名的比對上。上範例可以使用萬用字元改寫。 Example: #compiler CC = gcc #cflags CFLAGS = -Wall -ansi -s #object OBJS = main.o a.o b.o #include path INCLUDE_PATH = include all: myapp.exe app.doc myapp.exe: $(OBJS) [tab]$(CC) $(OBJS) -o $@ %.o: %.c [tab]$(CC) -I$(INCLUDE_PATH) $(CFLAGS) -c -o $@ $< 其實感覺沒有什麼差別對吧!但是後面討論專案的時候,就會感覺到差別了,其實萬用字元法則比較適合用於編譯一個大型的函式庫,而檔尾法則適合編譯一個目錄下面所有的檔案。 ※ 專案討論1 專案目錄配置如下: * + include (目錄) - a.h - b.h - c.h - d.h * - main.c * - a.c * - b.c * Makefile 檔案相依如下: * main.c include a.h d.h * a.c include a.h * b.c include b.h 建立要求如下: * 建立二元檔main * clean target移除全部目的檔 決定好上面的目錄結構之後還有檔案相依之後我們就開始來寫我們的makefile,但是有幾個巨集是非常建議,先寫出來的,而且請多多利用註解,可以幫助未來你看檔的時候不會不知道你在寫什麼東西。 Example: #compiler CC = gcc #cflags CFLAGS = -Wall -ansi -g #include path H_PATH = include #source SRCS = main.c a.c b.c #object OBJS = $(SRCS:.c=.o) all: main main: $(OBJS) [tab]$(CC) $(OBJS) -o $@ main.o: main.c $(H_PATH)/a.h $(H_PATH)/d.h [tab]$(CC) $(CFLAGS) -I$(H_PATH) -c -o $@ $< a.o: a.c $(H_PATH)/a.h [tab]$(CC) $(CFLAGS) -I$(H_PATH) -c -o $@ $< b.o: b.c $(H_PATH)/b.h [tab]$(CC) $(CFLAGS) -I$(H_PATH) -c -o $@ $< clean: [tab]-rm -f *.o 雖然是寫好了,但怎麼覺得多寫好多東西,所以呢?就要善用隱含法則幫我做最多事情,所以將上面改寫如下。 Example: #compiler CC = gcc #cflags CFLAGS = -Wall -ansi -g #include path H_PATH = include #source SRCS = main.c a.c b.c #object OBJS = $(SRCS:.c=.o) all: main main: $(OBJS) [tab]$(CC) $(OBJS) -o $@ main.o: main.c $(H_PATH)/a.h $(H_PATH)/d.h a.o: a.c $(H_PATH)/a.h b.o: b.c $(H_PATH)/b.h clean: [tab]-rm -f *.o 什麼??編不出來,它說我的標頭檔都找不到,哇列,該怎辦呢?因為我們使用隱含法則幫我們做最簡單的產生.o檔的方式,它只用在同一目錄下面的檔案,但是本目錄以外,就會找不到了,所以我們就使用檔尾法則來告訴make如果要產生.o檔的話,需要的標頭檔,應該在$(H_PATH),請去那裡找。 Example: #compiler CC = gcc #cflags CFLAGS = -Wall -ansi -g #include path H_PATH = include #source SRCS = main.c a.c b.c #object OBJS = $(SRCS:.c=.o) all: main main: $(OBJS) [tab]$(CC) $(OBJS) -o $@ .c.o: [tab]$(CC) $(CFLAGS) -I$(H_PATH) -c -o $@ $< main.o: main.c $(H_PATH)/a.h $(H_PATH)/d.h a.o: a.c $(H_PATH)/a.h b.o: b.c $(H_PATH)/b.h clean: [tab]-rm -f *.o 如果就可以成功的依照makefile所寫的內容一步步的把程式產生出來了,當然你也可以用喜歡萬用字元來做把.c.o改成%.o: %.c就可以了。 ※ 專案討論2 這邊我們要編譯不同目錄裡面的檔案,來建立我們自己的函式庫,而且將它移動到某個目錄之中,這是有點困難的,我們來看看以下的一些設定。 專案目錄配置如下: * + include (目錄) - gear_calendar.h - gear_io.h - gear_color.h - gear_string.h - trax_file.h - trax_name.h * + source (實作檔) - gear_calendar.c - gear_io.c - gear_string.c - trax_file.c - trax_name.c * + library (放函式庫的目錄) * Makefile 檔案相依如下: * gear_calendar.c include gear_calendar.h * gear_io.c include gear_io.h * gear_string.c include gear_string.h * trax_file.c include trax_file.h * trax_name.c include trax_name.h 建立要求如下: * 建立libtrax.a, libgear.a兩個函式庫 * clean target移除全部目的檔 * install target移動函式庫到library目錄下 因為目前要編的是函式庫,而且還不單只有一個函式庫,所以我們把標頭檔全都放在同一個資料夾裡面,而且屬於那個函式庫的檔案就以函式庫名稱為檔名的第一部份命名,如此我們方便分類我們的檔案,而實作檔就放在另一個目錄裡面,而且以其對應的標頭檔來命名實作檔,目錄檔案分配好之後我們就可以開始設計 makefile了。 Example: #compiler CC = gcc #option CFLAGS = -Wall -ansi -s #library path LIBRARY_PATH = library #include path INCLUDE_PATH = include #source path SOURCE_PATH = source #library name ALL_LIB = libgear.a libtrax.a #gear library need objects GEAR_OBJS = gear_calendar.o gear_io.o gear_string.o TRAX_OBJS = trax_name.o trax_file.o all: $(ALL_LIB) libgear.a: $(GEAR_OBJS) [tab]ar rcs $@ $(GEAR_OBJS) libtrax.a: $(TRAX_OBJS) [tab]ar rcs $@ $(TRAX_OBJS) gear_calendar.o: $(SOURCE_PATH)/gear_calendar.c $(INCLUDE_PATH)/gear_calendar.h [tab]$(CC) -I$(INCLUDE_PATH) $(CFLAGS) -c -o $@ $< gear_string.o: $(SOURCE_PATH)/gear_string.c $(INCLUDE_PATH)/gear_string.h [tab]$(CC) -I$(INCLUDE_PATH) $(CFLAGS) -c -o $@ $< gear_io.o: $(SOURCE_PATH)/gear_io.c $(INCLUDE_PATH)/gear_io.h [tab]$(CC) -I$(INCLUDE_PATH) $(CFLAGS) -c -o $@ $< trax_name.o: $(SOURCE_PATH)/trax_name.c $(INCLUDE_PATH)/trax_name.h [tab]$(CC) -I$(INCLUDE_PATH) $(CFLAGS) -c -o $@ $< trax_file.o: $(SOURCE_PATH)/trax_file.c $(INCLUDE_PATH)/trax_file.h [tab]$(CC) -I$(INCLUDE_PATH) $(CFLAGS) -c -o $@ $< install: $(ALL_LIB) [tab]-cp $(ALL_LIB) $(LIBRARY_PATH) clean: [tab]-rm -f $(GEAR_OBJS) $(ALL_LIB) 經過一番的辛苦,終於寫完了,從頭到尾一直在剪剪貼貼,我們可不可以用別的方法呢?用隱含法則好了,之先前的專案有說過了,隱含法則沒有 include path會造成編譯錯誤,那可以用.c.o的檔尾法則阿,但是又想到一個問題,檔尾法則只可以用在本目錄也,但是我們實作標(.c)和介面檔(.h)都放在不同的目錄也,我們該怎麼辦呢?那就用萬用字元法則吧! Example: #compiler CC = gcc #option CFLAGS = -Wall -ansi -s #library path LIBRARY_PATH = library #include path INCLUDE_PATH = include #source path SOURCE_PATH = source #library name ALL_LIB = libgear.a libtrax.a #gear library need objects GEAR_OBJS = gear_calendar.o gear_io.o gear_string.o TRAX_OBJS = trax_name.o trax_file.o all: $(ALL_LIB) libgear.a: $(GEAR_OBJS) [tab]ar rcs $@ $(GEAR_OBJS) libtrax.a: $(TRAX_OBJS) [tab]ar rcs $@ $(TRAX_OBJS) %.o: $(SOURCE_PATH)/%.c $(INCLUDE_PATH)/%.h [tab]$(CC) -I$(INCLUDE_PATH) $(CFLAGS) -c -o $@ $< install: $(ALL_LIB) [tab]-cp $(ALL_LIB) $(LIBRARY_PATH) clean: [tab]-rm -f $(GEAR_OBJS) $(ALL_LIB) 我們先來看看%.o: $(SOURCE_PATH)/%.c $(INCLUDE_PATH)/%.h以這個相依來說,make會先看看本目錄有沒有gear_calendar.o檔,如果沒有的話,會相依於我們同檔名在source/和include/的實作檔和介面檔,所以make可以找到實作檔且將它編譯出來。而%代表所有目錄下的檔名,在這裡有的檔名是 gear_calendar.o gear_io.o gear_string.o trax_name.o trax_file.o這些包在$(GEAR_OBJS)和$(TRAX_OBJS)兩個巨集裡面。 最後在透過ar指示將產生出來的.o檔依造我們設定的$(GEAR_OBJS)和$(TRAX_OBJS)兩個巨集,包在不同的static library之中,如果有函式庫不會包的話,可以參考本部落格的文章(Make Library for Linux),裡面會教三種函式庫(static library, shared library, dynamic library)。 因為只是產生兩個函式庫而已,但是我們還沒有真的安裝到library裡面可以讓別人使用,所以使用make install來安裝,請注意一下install: $(ALL_LIB)這個相依性是libgear.a libtrax.a要有存在,因為需要存在,所以它需要先去編出這兩個函式庫才可以安裝,所以不論你直接make install 或是make,它都會去產生兩個函式庫,而make install只是把函式庫產生出來,並且安裝到指定的目錄而已,其實你可以改成如下。 Example: install: all [tab]cp $(ALL_LIB) $(LIBRARY_PATH) 效果是一樣的,所以寫makefile最重要的是對此專案要很了解需要編什麼檔,什麼函式庫等等,而拿到別人的source通常也是透過看makefile可以了解它整個專案是怎麼產生出來,什麼介面和實作有相依性。 ※ 專案討論3 這邊我們寫了一個檔案來測試我們先前建立的函式庫,使用我們自己的介面,所以討論2的部份需要真的完全了解。 專案目錄配置如下: * + libgear (函式庫) + include (介面標頭檔) + source (介面實作檔) + library (函式庫使用檔) * space.c * Makefile 檔案相依如下: * space.c include gear_io.h gear_color.h gear_string.h 建立要求如下: * 建立space執行檔 * clean target移除全部目的檔 * 如果函式庫還沒有建立的話,要自動建立函式庫 這邊的要求還滿難的,因為如果函式庫libgear.a libtrax.a沒有的話根本不能編space.c所以產生函式庫是很重要的,所以我們要想個辦法讓它先去確認函式庫有沒有存在,有的話才可以直接連結使用,但是不在的話,也沒有關係,我們可以讓剛剛討論2的makefile幫我們來產生函式庫。 Example: #compiler CC = gcc #cflags CFLAGS = -Wall -ansi -g #include path INCLUDE_PATH = libgear/include #library path LIBRARY_PATH = libgear/library #use library USE_LIB = $(LIBRARY_PATH)/libgear.a $(LIBRARY_PATH)/libtrax.a #objects OBJECTS = space.o all: space space: $(OBJECTS) $(USE_LIB) [tab]$(CC) $(OBJECTS) $(USE_LIB) -o $@ $(USE_LIB): [tab]cd libgear; make install space.o: space.c [tab]$(CC) -I$(INCLUDE_PATH) $(CFLAGS) -c -o $@ $< clean: [tab]rm -f $(OBJECTS) 這邊最重要的就是確認有沒有libgear.a libtrax.a這兩個函式庫,沒有的話一切都不用搞了,所以設了一個巨集$(USE_LIB)因為路徑不一樣,所以要自己設定好,而在連結的時候需要$(USE_LIB)如果它不存在的話,就成為一個目標了,而這個目標就是叫libgear目錄下面的makefile去做,把libgear.a libtrax.a產生出來,而其它的部份怎麼編譯space.o我相信大一定已經非常熟悉了,所以這邊就不講下去了。 Reference: http://hsian-studio.blogspot.com/2008/09/makefile_08.html

Make Library for Linux

使用函式庫(API)讓我們開發程式速度增加,而且小細節的部份不用去特別注意,因為函式庫已經幫你解決掉了,自己辛苦寫了那麼多的程式,一定也會想有自己的函式庫,方便自己以後開發程式使用,所以這邊講解在Linux上所用到的函式庫有那些,以及如何使用。

※ 預先了解觀念

* 內外部連結(external linkage, internal linkage)
* 多檔案程式製成
* 基本gcc的使用
* Linux目錄樹

※ 將學會的觀念

* static library (.a library)
* shared library (.so library)
* dynamic loader (DL library)

※ 練習檔

Example:

test1.c for share and static library

#include
#include
#include "adio.h"
#include "adstring.h"
int main()
{
char input[21];
char buffer[11];
char buf[11];
const char* str = "hello world!!";
adstring_strcpy(buffer, str, 11);
puts(buffer);
adio_fgets(input, 21, stdin);
puts(input);
adstring_strcpy(buffer, input, 5);
puts(buffer);
int a = 12345;
puts(adstring_itoa(buf, a, 11));
puts(buf);
return 0;
}

test2.c for dynamic loader

#include
#include
#include
#include
int main()
{
void* handle;
char* (*so_strcpy)(char*, const char*, int);
char* (*so_gets)(char*, int);
char* (*so_itoa)(char*, int, int);
char* error;
handle = dlopen("./libadlib.so", RTLD_LAZY);
if (!handle)
{
puts (dlerror());
exit(1);
}
so_strcpy = dlsym(handle, "adstring_strcpy");
so_gets = dlsym(handle, "adio_fgets");
so_itoa = dlsym(handle, "adstring_itoa");
char input[21];
char buffer[11];
char buf[11];
const char* str = "hello world!!";
so_strcpy(buffer, str, 11);
puts(buffer);
so_gets(input, 21);
puts(input);
so_strcpy(buffer, input, 5);
puts(buffer);
int a = 12345;
so_itoa(buf, a, 11);
puts(buf);
dlclose(handle);
return 0;
}

adio.h for static, shared include

#ifndef ADVENCE_IO_H
#define ADVENCE_IO_H
#include
char* adio_fgets(char* buf, int num, FILE* fp);
void adio_stdinclean(void);
#endif

adstring.h for static, shared include

#ifndef ADVENCE_STRING_H
#define ADVENCE_STRING_H
char* adstring_strcpy(char* to, const char* from, int num);
char* adstring_itoa(char* to, int from, int num);
#endif

adio.c for make lib

#include
#include
char* adio_gets(char* buf, int num, FILE* fp)
{
char* find = 0;
fgets(buf, num, stdin);
if ((find = strrchr(buf, '\n')))
{
*find = '\0';
}
else
{
while (fgetc(fp) != '\n');
}
return buf;
}
void adio_stdinclean()
{
while (getchar() != '\n');
}

adstring.c for make lib

#include
#include
char* adstring_strcpy(char* to, const char* from, int num)
{
int size = num-1;
strncpy(to, from, size);
if (strlen(from) >= size)
{
to[size] = '\0';
}
return to;
}
char* adstring_itoa(char* to, int from, int num)
{
char tmp[11];
sprintf(tmp, "%d", from);
adstring_strcpy(to, tmp, num);
return to;
}

※ 靜態函式庫(static library)

所謂的靜態函式庫,簡單講是把一堆object檔用ar(archiver)包裝集合起來,檔名以「.a」結尾。優點是執行效能通常會比後兩者快,而且因為是靜態連結,所以不易發生執行時找不到library或版本錯置而無法執行的問題。缺點則是檔案較大,維護度較低;例如library如果發現bug需要更新,那麼就必須重新連結執行檔。

* 連結時期在程式還沒有開始執行就已連結
* 執行檔因為包含了函式庫,所以檔案較大
* 執行檔因為不用去尋找函式庫,所以執行較快
* 函式庫如果更新的話,使用到此函式庫所有的檔案要全部重新連結

Example:
Make static library

gcc adio.c adstring.c -Wall -c (編出adio.o adstring.o)
ar rcs libadlib.a adio.o adstring.o (包成libadlib.a檔)

如此我們可以建立由adio.o adstring.o所產生的static library(.a)其中檔名libadlib.a前面的「lib」和後面的「.a」是不能少的,因此你的靜態函式庫命名要以lib(xxxxx).a 才行,再使用ar指令將.o檔包在一個函式庫裡面,接著我們來看使用方法。

Example:
Use static library

gcc test.c -I. -L. -ladlib -o test1

其中-I是代表目前的標頭檔尋找位子,-L是代表目前的函式庫尋找位子,-ladlib是使用函式庫libadlib.a所以剛剛說的lib(xxxx).a現在看的出來只取出xxxx前後都不見了,如此可以成功使用static library了。

一般來說static library使用的目的是不想給別人你的source所以你都包好成為.a檔,之後只需要傳給別人你的標頭檔,和你的靜態函式庫.a別人就可以直接使用了。

※ 共享函式庫(shared library)

Shared library會在程式執行起始時才被自動載入。因為程式庫與執行檔是分離的,所以維護彈性較好。有兩點要注意,shared library是在程式起始時就要被載入,而不是執行中用到才載入,而且在連結階段需要有該程式庫才能進行連結。首先有一些名詞要弄懂,soname、 realname與linkername。

soname用來表示是一個特定library 的名稱,像是libmylib.so.1 。前面以「lib」開頭,接著是該library 的名稱,然後是「.so」接著是「版號」,用來表名他的介面;如果介面改變時,就會增加版號來維護相容度。

realname 是實際放有library程式的檔案名稱,後面會再加上minor 版號與release 版號,像是libmylib.so.1.0.0 。一般來說,最尾碼的release版號用於程式內容的修正,介面完全沒有改變。中間的minor用於有新增加介面,但相舊介面沒改變,所以與舊版本相容。最前面的version版號用於原介面有移除或改變,與舊版不相容時。

linkername是用於連結時的名稱,是不含版號的soname ,如: libmylib.so。通常linker name與real name是用ln 指到對應的real name ,用來提供彈性與維護性。

* 連結時期在程式開始時連結載入
* 執行檔因為不含有函式庫所以比較小
* 執行檔需要尋找所使用的.so檔所以比較慢
* 函式庫若有更改,重新建立函式庫即可,程式方面不用重編譯
* 可具有函式庫版本管理

Example:
Make shared library

gcc adio.c adstring.c -c -Wall -fPIC (編出adio.o adstring.o)
gcc adio.o adstring.o -shared -Wl,-soname,libadlib.so.1 -o libadlib.so.1.0.1 (編出libadlib.so.1.0.1共享函式庫的檔)
ln -s libadlib.so.1 libadlib.so (連結時的linker name)
ldconfig -n . (產生libadlib.so.1 -> libadlib.so.1.0.1的s-link)

編譯時要加上-fPIC用來產生position-independent code。也可以用-fpic參數。(不太清楚差異,只知道-fPIC 較通用於不同平台,但產生的code較大,而且編譯速度較慢)。而-shared 表示要編譯成shared library,-Wl 用於參遞參數給linker,因此-soname與libmylib.so.1會被傳給linker處理,-soname用來指名soname 為libadlib.so.1,library會被輸出成libadlib.so.1.0.1 (也就是real name),若不指定soname 的話,在編譯結連後的執行檔會以連時的library檔名為soname,並載入他。否則是載入soname指定的library檔案,所以我們產生以下各檔。

* soname: libadlib.so.1 (由ldconfig -n . 產生)
* realname: libadlib.so.1.0.1 (由gcc -shared -Wl .....產生)
* linkername: libadlib.so (自己用ln -s所建立的)

其中我們要將上面三個soname, realname, linkername用符號連結串起來libadlib.so -> libadlib.so.1 -> libadlib.so.1.0.1,為什麼呢?因為當我們連結使用的時候會連結libadlib.so而在程式執行的時候,會因為soname的關系 ld(連結器)會去尋找libadlib.so.1,而透過符號連結,可以由libadlib.so.1找到libadlib.so.1.0.1我們真正的共享函式庫檔,這樣做有一些好處。

* 連結的時候連結libadlib.so沒有版次的問題,因為會自動連到最新的版次,如果那天有更新的soname的版次出來的話只需要更改libadlib.so -> libadlib.so.2新的版次即可
* 如果共享函式庫做一點點小修正,而後只要增加共享函式庫的編號即可,如libadlib.so.1.0.2表示修正過,因此目錄下會有 libadlib.so.1.0.1(舊版本)libadlib.s0.1.0.2(新版本),但是透過「ldconfig -n .」會自動產生libadlib.so.1 -> libadlib.so.1.0.2的符號連結,不過先決條件是你此版的soname還是libadlib.so.1,而使用了ldconfig才會自動產生這樣的符號連結
* ldconfig是專門用在自動產生soname對映到最近期產生,或是編號最新,共享函式庫的符號連結檔

產生出來了libadlib.so(s-link), libadlib.so.1(s-link), libadlib.so.1.0.1(share library)我們來看看應該如何使用這些檔案。

Example:
Use shared library

gcc test.c -I. -L. -ladlib -o test2 (和static library沒有兩樣)

雖然建立好像和static library差別不大,如果目錄下同時有static與shared library的話,會以shared為主,使用-static參數可以避免使用shared連結,但是由上例編完後開始執行的時候應該會告訴你,找不到你的共享函式庫,嘿嘿!!因為路徑的問題,預設下只會找尋/usr/lib或是/usr/local/lib所以當然啦。你都放在自己的目錄當然找不到,我們可以用ldd來觀查看看。

Example:
查看test2中使用那些shared library

ldd ./test2 (會告訴你libadlib.so.1找不到啦)
(libadlib.so.1 => not found)

因此我們有幾個解決的辦法,來解決找不到你的shared library的方法。

1. 把libadlib.so(linkername), libadlib.so.1(soname), libadlib.so.1.0.1(realname)全都複製一份到/usr/lib中
2. /etc/ld.so.conf,加入一個新的library搜尋目錄,並執行ldconfig更新快取
3. 設定LD_LIBRARY_PATH環境變數來搜尋library,如:export LD_LIBRARY_PATH=.

以上的三種方式都可以讓你的shared library被正確的找到,但是我們通常使用第一種方法,因為這樣你的函式庫就可以被使用此系統的所有人共享了,而第三種方法通常都是用在暫時搬到某台機器上測式時暫時讓目前的目錄會被搜尋到。

※ 動態載入函式庫 (dynamic loading library)

其實動態載入函式庫不是一個檔案或是程式,而是一個技術,只有當你的函式庫為共享函式庫的時候,才可以使用dynamic loading這樣的技術,而此技術是不需要在連結的時候告訴ld說我們要用那個函式庫,而是在程式執行期間,執行某一行,真的需要用到那個函式庫裡面的某個函式的時候,才去和此函式庫連結,而且只單取用其中的某個函式,如此對系統需求更是少,但是同樣的因為載入的關係,會造成程式執行速度比較慢,此技術類似windows上的DLL動態連結檔。

這個技術比較有一點難度的就是,它透過一些函式來達到這樣的效果,動態載入是透過dl function的一套函式來實作,以下列出這些常使用的dl函式,而它的標頭檔為dlfcn.h。

void* dlopen(const char* filename, int flag);
(開啟載入filename指定的library載入執行程式中)

* filename 要載入的.so檔名(含路徑)
* flag 載入的方式(RTLD_LAZY)
* 回傳函式庫的記憶體位址

void* dlsym(void* handle, const char* symbol);
(取得symbol指定的函式名稱,所指向的函式記憶體位址)

* handle 由dlopen所取得的函式庫記憶體位址
* symbol 要使用此函式庫的某個函式名稱
* 回傳函式的記憶體位址

int dlclose(void *handle);
(關閉dlopen開啟的handle)

char *dlerror(void);
(傳回最近所發生的錯誤訊息)

以上的test2.c是由test1.c改變而來的,結果都是完全一樣的,但是test2.c是使用dynamic loading的技術完成的,此範例可以看到如果使用上面說的四個函式,程式會寫了之後,還需要怎麼連結才行,因此來我們連結看看吧!假設上面的 shared library所建立的三個檔libadlib.so, libadlib.so.1, libadlib.so.1.0.1都在目前的目錄下面。

Example:
dynamic loading shared library

gcc test2.c -ldl -o test3 (dl是dlfcn.h的函式庫)

對於動態載入沒有路徑上的問題,只要你在dlopen裡面的filename路徑設定是正確的,那就可以開啟使用你的shared library達到動態載入的能力,它和之前使用函式庫的方法有一些不同。

* 在test2.c中並不用含入adio.h adstring.h標頭檔
* 使用函都是透過dlsym回傳的函式指標來使用函式庫中的函式
* 在編譯的時候非常簡單,只要-ldl即可
* 對於函式的介面要非常清楚(不然怎麼寫函式指標)

※ 總結

使用static library, shared library, dynamic loading library都各有好處,通常如果小程式的話製成static library就很好用了,編譯的時候-L. -ladlib就連好了不用考慮會找不到函式庫的問題,但是如果你的函式庫常常要改動的話,使用shared library比較好,因為透過ldconfig可以為你的soname自動建立符號連結,指向你的realname的函式庫檔,所以只要編好新的 shared library就可以換掉舊的,而且程式不用重編譯,但是麻煩在於如果要移到別台機器上,移動過去配置就要花上不少時間,有點麻煩。最後使用 dynamic loading這是個技術,只能用在shared library,讓你單獨用到某個函式庫中的幾個函式,但是麻煩的地方是,載入你全都要自已來控制,自己撰寫程式碼,透過dlopen, dlsym, dlclose等等來控制。

Reference:
http://hsian-studio.blogspot.com/2008/09/make-library-for-linux.html

在X環境安裝scim輸入法

在X環境安裝scim輸入法
by Anati
linux的輸入法有非常多種,但是gcin和scim比較多人使用,而我比較喜歡使用scim當我的輸入法,而且它支援無蝦米輸入法的tables,這篇就來談談如何安裝scim+無蝦米輸入法。

首先需要先安裝幾個套件scim scim-tables-zh im-switch
無蝦米輸入法下載點 http://moon.cse.yzu.edu.tw/~s932361/linux/liu_scim.tgz

#debfoster scim (scim輸入法主程式)
#debfoster scim-tables-zh (scim中文各種輸入法)
#debfoster im-switch (各種輸入法的選擇程式,自動啟動輸入法引擎)
im-switch可參考http://www.debian.org.hk/story/im-switch

以上都安裝好之後,接下來要安裝無蝦米輸入法了
#wget http://moon.cse.yzu.edu.tw/~s932361/linux/liu_scim.tgz
#tar zxvf liu_scim.tgz (解開之後裡面有兩個檔案,一個是noseeing.bin and liu5.png)
#cd liu_scim
#cp liu5.png /usr/share/scim/icons/
#cp noseeing.bin /usr/share/scim/tables/liu5.bin (我喜歡自己更名啦,其實換不換都可以)
以上工作完成之後就裝好無蝦米輸入法了

這時候重新啟動X,進入之後開始對scim做細部設定,所以執行scim-setup會有視窗跳出來勾一勾就設定好了,裡面可以設定輸入法快速鍵,我是習慣用ctrl+alt+方向鍵右,來選擇入法,如此就可以在X環境中快樂的使用各種輸入法了。

Reference:
http://hsian-studio.blogspot.com/2008/09/xscim.html

Tuesday, September 13, 2011

clear vim backup file ~ extension filename

# find /www/drupal_center/sites/all/modules/custom -name '*~' -type f -print0 | xargs -0 -I @ rm @

Thanks for the information, it helped out a lot. I would like to add a few comments about the above information.

set nobackup #no backup files

Having this set will not leave any additional file(s) around after having closed VIM. This is what most people might be looking to have set.

set nowritebackup #only in case you don't want a backup file while editing

I do not set this one. The default is :set writebackup This will keep a backup file while the file is being worked. Once VIM is closed; the backup will vanish.

Where nowritebackup changes the default "save" behavior of Vim, which is:
1. write buffer to new file
2. delete the original file
3. rename the new file

and makes Vim write the buffer to the original file (resulting in the risk of destroying it in case of an I/O error). But you prevent "jumping files" on the Windows desktop with it, which is the primary reason for me to have nowritebackup in place.

set noswapfile #no swap files

From the VIM help file.
- Don’t use this for big files.
- Recovery will be impossible!
In essence; if security is a concern, use noswapfile. Keep in mind that this option will keep everything in memory

I think the better solution is to place these lines in your vimrc file:

" controls where backup files (with ~ extension by default) go.
set backupdir=~/.vimbak

" The 'directory' option controls where swap files go.
set directory=~/.vimbak

You have to create a directory in your home directory called vimtmp for this to work.

That way you get the benefit of both worlds, you don't have to see the files, but if something does get futzed you can go get your backup file from vimtmp. Don't forget to clean the directory out every now and then.

And you can also set a different backup extension and where to save those backup (I prefer ~/.vimbackups on linux). I used to use "versioned" backups, via:

au BufWritePre * let &bex = '-' . strftime("%Y%m%d-%H%M%S") . '.vimbackup'
This sets a dynamic backup extension (ORIGINALFILENAM-YYYYMMDD-HHMMSS.vimbackup).

you might want to include how to set up the back up directory, i.e. putting set backupdir=~/.vimbackups in your ~/.vimrc

The vim help fully describes this technique. See :help backupext for file extension which is appended to a file name to make the name of the backup file.

http://stackoverflow.com/questions/607435/why-does-vim-save-files-with-a-extension

http://stackoverflow.com/questions/607435/why-does-vim-save-files-with-a-extension

Friday, September 9, 2011

Upgrade Using Newer Versions of jQuery with Drupal 6

There are a lot of forum posts and issue queues revolving around the need to use a newer version of jQuery on a Drupal 6.x site. There are several ways to achieve this, such as using the jQuery Update module, but I'm going to focus on the recommended method of running 2 versions of jQuery side-by-side - the jQuery noConflict() function.

The Problem
Your custom theme, dropdown menu, slideshow, or other jQuery dependant scripts require jQuery 1.4, jQuery UI 1.8, or some other jQuery version or component. Drupal 6 ships with jQuery 1.2.6. You could just replace the jquery.js file in example.com/misc with a newer version, but you'd run into some issues, such as:

Useful UI features like draggable tables and ajax search will stop working
Contributed modules that use jQuery assume you are using 1.2.6
After updating Drupal to a newer version, your custom jquery.js file would be overwritten with 1.2.6
The Solution
jQuery already has the functionality to run along side a different version of jQuery (or, really, along side any other JavaScript library that uses the $ symbol as a function or variable). This is the noConflict() function. You can view the API page here: http://api.jquery.com/jQuery.noConflict/

To use this functionality within your project, simply tell the browser about your new jQuery library and that you'll be referencing it via your custom noConflict identifier:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
var $jq = jQuery.noConflict();
</script>
Example: my-theme/page.tpl.php
<head>
<title><?php print $head_title; ?></title>
<?php print $head; ?>
<?php print $styles; ?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
var $jq = jQuery.noConflict();
</script>
<?php print $scripts; ?>
</head>
With this method jQuery 1.2.6 is loading with every page, and can be used by Views, Draggable.js, any contributed Ajax calls, etc. Also, jQuery 1.4.4 (as in this example) is also loading and the 2 are not interfering with each other. Another point worth adding is that 1.4.4 is being loaded from the Google CDN - one less file for your audience to download from your server.

Using noConflict in your scripts
To execute scripts with the new version of jQuery you simply need to use $jq instead of $ in your scripts, for example:

$jq(document).ready(function(){
$jq('#my-div').function();
});
Using noConflict in contributed scripts
You can easily modify an external script to use your custom noConflict identifier. For example, the Qtip library depends on jQuery 1.5. You could include it via the above methods and then alter jquery.qtip.js, replacing the first line

(function($, window, undefined) {

with

(function($jq, window, undefined) {

Reference:
http://drupal.org/node/1058168

Thursday, September 8, 2011

Run JQuery in the context of another frame iframe

window.parent.frames[0].document

window.parent.theFrameID.document // which is more verbose

$('#test', window.parent.theFrameID.document).val('this is a test');

Reference:
http://stackoverflow.com/questions/539504/run-jquery-in-the-context-of-another-frame

Wednesday, September 7, 2011

SSH Client Putty, OmNiTTY, PuTTYCS and PuTTY Connection Manager

OmNiTTY - is a curses-based program that allows one to log into several machines simultaneously and interact with them, selectively directing input to individual machines or groups of selected machines.

You can run both line-oriented and screen oriented in the target machines, because Omnitty has built-in terminal emulation capability. When the window is large enough, Omnitty also displays a "summary area" for each machine, in which it shows what the latest output from the machine was, so you can have an idea of what is going on in each machine.

http://omnitty.sourceforge.net/
OmNiTTY - 同步作業的好工具

PuTTYCS - PuTTY Command Sender - is a small windows application that is intended
to be used along with multiple instances of PuTTY
(including PuTTYtel, TuTTY, and PieTTY). It's purpose is
to send the same command to each PuTTY window. It is can
be useful for copying files, starting and stopping processes,
and examining logs on multiple servers.

http://www.millardsoftware.com/puttycs

PuTTY Connection Manager - is a free PuTTY Client Add-on for Windows platforms which goal is to provide a solution for managing multiple PuTTY instances. This is one of the most important missing feature of PuTTY.

http://puttycm.free.fr/cms/