㈠ 這個linux程序的Makefile疑問求解
你要讀懂這個Makefile,要查兩樣東西,一樣是gcc文檔,一樣是Makefile的文檔。
先來說gcc的編譯選項問題:
-Wall是做檢查,比如語法錯誤啊,指針不一致,參數沒有用到之類的錯誤
關於 -Wno-format,在GCC的編譯選項中有以下解釋,你可以讀一讀,具體的方法你去man gcc,可以查到相關信息
-Wno-format-extra-args
If -Wformat is specified, do not warn about excess arguments to a
"printf" or "scanf" format function. The C standard specifies that
such arguments are ignored.
Where the unused arguments lie between used arguments that are
specified with $ operand number specifications, normally warnings
are still given, since the implementation could not know what type
to pass to "va_arg" to skip the unused arguments. However, in the
case of "scanf" formats, this option will suppress the warning if
the unused arguments are all pointers, since the Single Unix
Specification says that such unused arguments are allowed.
-Wno-format-zero-length (C and Objective-C only)
If -Wformat is specified, do not warn about zero-length formats.
The C standard specifies that zero-length formats are allowed.
關於-g,用於GDB調試,這個不用再說了吧。
-DDEBUG
實際上是用-D定義了一個名叫DEBUG的宏,初始值為1。
所以你的Makefile下面會有這么一段
ifeq (YES, ${DEBUG})
CFLAGS := ${DEBUG_CFLAGS}
CXXFLAGS := ${DEBUG_CXXFLAGS}
LDFLAGS := ${DEBUG_LDFLAGS}
else
CFLAGS := ${RELEASE_CFLAGS}
CXXFLAGS := ${RELEASE_CXXFLAGS}
LDFLAGS := ${RELEASE_LDFLAGS}
endif
表示如果是YES的值與DEBUG的值相等的話(不加$()指數字的值相等)用於調試手段,則會調用調試用的相關變數,如果是正式發行版,則會用REALEASE的相關變數,
你的第二個問題是關於Makefile的函數的,
basename表示取前綴函數,比如basename a.out 返回值為a,
而addsuffix是加後綴的函數addsuffix .o,a 返回值為a.o
不過建議你去看看Makefile的手冊。
㈡ linux下一個版本,有Makefile,請問,如果分別編譯成debug和release版本,是輸入make -release么
一般,在開發測試階段用debug版本,而上線發布用release版本。
使用Makefile定製編譯不同版本,避免修改程序和Makefile文件,將會十分方便。
讀了一些資料,找到一個解決方法,Makefile預定義宏與條件判斷,結合make預定義變數,進行條件編譯。
比如,有一個test.cpp,包含這段代碼
#ifdef debug
//your code#endif
你希望在debug版本要執行它,在release版本不執行。
我們可以寫這樣的一個Makefile:
1 ver = debug
2
3 ifeq ($(ver), debug)
4 ALL: test_d
5 CXXFLAGS = -c -g -Ddebug
6 else 7 ALL: test_r
8 CXXFLAGS = -c -O3
9 endif
10
11 test_d: test.do12 g++ -o $@ $^
13
14 test_r: test.ro
15 g++ -o $@ $^
16
17 %.do: %.cpp
18 g++ $(CXXFLAGS) $< -o $@
19
20 %.ro: %.cpp
21 g++ $(CXXFLAGS) $< -o $@
簡單說一下,Makefile根據ver的不同定義了不同的編譯選項CXXFLAGS與輸出程序ALL,
debug版本輸出程序是test_d,release版本輸出程序是test_r
debug版本編譯選項是"-c -g -Ddebug",release版本編譯選項是"-c -O3"
debug版本object文件後綴是".do",release版本object文件後綴是".ro"
debug版本編譯選項使用"-D"定義宏debug,使得your code能夠執行。
不同版本的編譯選項、object文件、輸出程序均不同,所以可以同時編譯兩個版本的程序,互不影響。
Makefile執行時,首先判斷ver變數,如果ver的值是debug,編譯debug版,否則編譯release版。當然,默認情況下是編譯debug版的。
如果想編譯release版,要怎麼做?
只要在執行make時,對ver變數賦值,使得ver的值不為debug,比如# make ver=release