linux 用户空间通过makefile向程序传递参数
2013-06-24 17:37
239 查看
转自:http://blog.chinaunix.net/uid-23767307-id-1991981.html
一. 用户空间
因为实际上进行预处理的只是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;
}
Makefile:
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]$
通过以上的简单例子说明如何通过宏开关进行条件编译。
一. 用户空间
因为实际上进行预处理的只是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;
}
Makefile:
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]$
通过以上的简单例子说明如何通过宏开关进行条件编译。
相关文章推荐
- linux 用户空间通过makefile向程序传递参数
- 通过给事件处理程序传递this参数,获取事件源对象的引用。单机提交按钮时在信息框中显示用户输入的字符。
- 对Linux用户空间与内核空间数据传递的几点理解和总结(ZZ)
- 【从零开始,从内核驱动驱动到用户空间调用】编写第一个linux驱动,通过端口访问I/O寄存器。
- Linux tty驱动学习 - 在用户空间设置串口参数操作流程
- “传递/转发”可变参数并通过printf记录程序日志,彻底告别vsnprintf!:)
- 在 Linux 下用户空间与内核空间数据交换的方式,第 1 部分: 内核启动参数、模块参数与sysfs、sysctl、系统调用和netlink
- 用户空间与内核空间数据交换程序(netlink) -- linux内核
- Linux的内核空间和用户空间通过内存共享来进行通信的实现
- Linux 脚本和程序对SIGINT的处理方案,脚本通过kill给程序传递信号
- linux 用户空间和内核空间数据传递
- (转载) 在 Linux 下用户空间与内核空间数据交换的方式,第 1 部分: 内核启动参数、模块参数与sysfs、sysctl、系统调用和netlink
- 通过JNI在JAVA与C程序中传递参数
- 嵌入式 Linux 通过信号机制在进程间传递参数中的几个主要函数解析
- Linux下C程序设计(4)----操作环境变量 、程序传递参数getopt getopt_long操作、获取时间
- 在 Linux 下用户空间与内核空间数据交换的方式,第 1 部分: 内核启动参数、模块参数与sysfs、sysctl、系统调用和netlink
- 在 Linux 下用户空间与内核空间数据交换的方式,第 1 部分: 内核启动参数、模块参数与sysfs、sysctl、系统调用和netlink
- [Linux应用]通过sysfs在用户空间使用GPIO中断
- mfc通过消息传递参数进行程序间通信
- 在 Linux 下用户空间与内核空间数据交换的方式,第 1 部分: 内核启动参数、模块参数与sysfs、sysctl、系统调用和netlink