linux 用户空间通过makefile向程序传递参数

来源:互联网 发布:网络对大学生的好处 编辑:程序博客网 时间:2024/06/06 12:25

一. 用户空间

     因为实际上进行预处理的只是Gcc工具,而make工具只是一个解决依赖关系的工具。所以问题就简化成如何通过make向gcc传递参数。
     通过简单的例子来说明:
hello.c
  1. #include <stdio.h>

  2. void main(void) {
  3. #ifdef DEBUG
  4.      printf("you ask for debug!\n");
  5. #endif
  6.      printf("we must say goodbye\n");
  7.      return;
  8. }
Makefile:
  1. ifeq ($(DEBUG),y)
  2. CFLAGS := $(CFLAGS) -DDEBUG
  3. endif

  4. hello: hello.c
  5. $(CC) $(CFLAGS) $< -o $@
执行过程:
  1. [ville@localhost test]$ ls
  2. hello.c Makefile
  3. [ville@localhost test]$ make
  4. cc hello.c -o hello
  5. [ville@localhost test]$ ./hello
  6. we must say goodbye
  7. [ville@localhost test]$ rm hello
  8. [ville@localhost test]$ ls
  9. hello.c Makefile
  10. [ville@localhost test]$ make DEBUG:=y
  11. cc -DDEBUG hello.c -o hello
  12. [ville@localhost test]$ ./hello
  13. you ask for debug!
  14. we must say goodbye
  15. [ville@localhost test]$
通过以上的简单例子说明如何通过宏开关进行条件编译。
0 0
原创粉丝点击