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


转自:http://blog.chinaunix.net/uid-23767307-id-1991981.html



一. 用户空间

     因为实际上进行预处理的只是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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值