一. 用户空间
因为实际上进行预处理的只是Gcc工具,而make工具只是一个解决依赖关系的工具。所以问题就简化成如何通过make向gcc传递参数。通过简单的例子来说明:
hello.c
- #include <stdio.h>
- void main(void) {
- #ifdef DEBUG
- printf("you ask for debug!\n");
- #endif
- printf("we must say goodbye\n");
- return;
- }
- ifeq ($(DEBUG),y)
- CFLAGS := $(CFLAGS) -DDEBUG
- endif
- hello: hello.c
- $(CC) $(CFLAGS) $< -o $@
- [ville@localhost test]$ ls
- hello.c Makefile
- [ville@localhost test]$ make
- cc hello.c -o hello
- [ville@localhost test]$ ./hello
- we must say goodbye
- [ville@localhost test]$ rm hello
- [ville@localhost test]$ ls
- hello.c Makefile
- [ville@localhost test]$ make DEBUG:=y
- cc -DDEBUG hello.c -o hello
- [ville@localhost test]$ ./hello
- you ask for debug!
- we must say goodbye
- [ville@localhost test]$