Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1481496
  • 博文数量: 228
  • 博客积分: 1698
  • 博客等级: 上尉
  • 技术积分: 3241
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-24 21:49
个人简介

Linux

文章分类

全部博文(228)

文章存档

2017年(1)

2016年(43)

2015年(102)

2014年(44)

2013年(5)

2012年(30)

2011年(3)

分类: LINUX

2012-03-29 15:33:58

Linux下特别的东西特别的多,比如说定义,看Linux内核代码,无数个宏开关,当然,这并不是Linux的过错,要知道Linux内核特性的开启与否都要靠这些宏定义。
在判断一个宏定义是否开启时,各人有各人的办法,最简单的当然是直接看代码,但这不一定准确,因为有可能此处define的宏在别处又undef了,在代码实际用到这个宏的地方,这个宏到底是否还存在呢?所以,最保险的方法就是在实际用到这个宏的地方做判断,此时可利用指令,在编译时就可以准确判断出结果,直接看示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Makefile
 
MDIR = $(shell pwd)
 
ifeq (, $(KSRC))
    KSRC := /usr/src/linux-`uname -r`
endif
 
ifeq (, $(PROJECT_DIR))
    PROJECT_DIR := $(PWD)/../
endif
 
module := test_macro
 
obj-m := $(module).o
 
srcs =  $(wildcard, *.c)
 
$(module)-objs := $(addsuffix .o, $(basename $(srcs)))
 
EXTRA_CFLAGS += -g $(FLAG) -I$(PROJECT_DIR)/inc -I${SHAREDHDR} -I$(KERNELHDR) -O2 -D__KERNEL__ -DMODULE $(INCLUDE) -DEXPORT_SYMTAB
 
TARGET = $(module).ko
 
all:
    make -C $(KSRC) M=$(MDIR) modules
 
debug:
    make EXTRA_FLAGS="${EXTRA_CFLAGS} -DDEBUG" -C $(KSRC) M=$(MDIR) modules
 
clean:
    make -C $(KSRC) M=$(MDIR) clean
 
install: all
    cp -f $(TARGET) $(INSTALL_DIR)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
 * test_macro.c
 */
#include
#include
#include
#include
#include
 
#define HAVE_TX_MQ
 
static int __init test_macro_init(void)
{
#ifndef HAVE_TX_MQ
 
   #error HAVE_TX_MQ not define
 
#endif
 
    return 0;
}
 
static void __exit test_macro_fini(void)
{
 
    //Do Nothing
    return;
}
 
module_init(test_macro_init);
module_exit(test_macro_fini);
 
MODULE_LICENSE("GPL");
MODULE_AUTHOR("lenky0401 at gmail dot com");

由于在源文件test_macro.c的前面定义了HAVE_TX_MQ宏,所以编译正常:

1
2
3
4
5
6
7
8
9
10
[root@localhost k]# make
make -C /usr/src/linux-`uname -r` M=/home/lenky/k modules
make[1]: Entering directory `/usr/src/linux-2.6.38.8'
  CC [M]  /home/lenky/k/test_macro.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/lenky/k/test_macro.mod.o
  LD [M]  /home/lenky/k/test_macro.ko
make[1]: Leaving directory `/usr/src/linux-2.6.38.8'
[root@localhost k]#

假若把“#define HAVE_TX_MQ”换成“#undef HAVE_TX_MQ”,再编译则会报错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@localhost k]# sed --in-place -e 's/#define/#undef/' test_macro.c
[root@localhost k]# head -n 15 test_macro.c
/**
 * test_macro.c
 */
#include
#include
#include
#include
#include
 
#undef HAVE_TX_MQ
 
static int __init test_macro_init(void)
{
#ifndef HAVE_TX_MQ
 
[root@localhost k]# make
make -C /usr/src/linux-`uname -r` M=/home/lenky/k modules
make[1]: Entering directory `/usr/src/linux-2.6.38.8'
  CC [M]  /home/lenky/k/test_macro.o
/home/lenky/k/test_macro.c:16:5: error: #error HAVE_TX_MQ not define
make[2]: *** [/home/lenky/k/test_macro.o] Error 1
make[1]: *** [_module_/home/lenky/k] Error 2
make[1]: Leaving directory `/usr/src/linux-2.6.38.8'
make: *** [all] Error 2
[root@localhost k]#

这样,我们就能准备的判断某处使用的宏是否已经定义,方便我们在不实际执行代码的情况下知道程序流程走向,帮助理解源代码。

转载请保留地址:



阅读(3580) | 评论(0) | 转发(1) |
0

上一篇:PCIE简单介绍

下一篇:如何高效的访问内存

给主人留下些什么吧!~~