㈠ 这个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