Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6379307
  • 博文数量: 579
  • 博客积分: 1548
  • 博客等级: 上尉
  • 技术积分: 16634
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-12 15:29
个人简介

http://www.csdn.net/ http://www.arm.com/zh/ https://www.kernel.org/ http://www.linuxpk.com/ http://www.51develop.net/ http://linux.chinaitlab.com/ http://www.embeddedlinux.org.cn http://bbs.pediy.com/

文章分类

全部博文(579)

文章存档

2018年(18)

2015年(91)

2014年(159)

2013年(231)

2012年(80)

分类: C/C++

2012-12-14 16:25:16

预编译(预处理)就是在正式编译之前的,为正式编译做准备,处理以#开头的指令。


1, 在看linux内核代码时会看到某些结构体的定义中包含宏定义:

1
2
3
4
5
6
7
8
9
10
11
12
13
struct i2c_msg {
    __u16 addr;    /* slave address            */
    __u16 flags;
#define I2C_M_TEN        0x0010    /* this is a ten bit chip address */
#define I2C_M_RD        0x0001    /* read data, from slave to master */
#define I2C_M_NOSTART        0x4000    /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_REV_DIR_ADDR    0x2000    /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_IGNORE_NAK    0x1000    /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_NO_RD_ACK        0x0800    /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_RECV_LEN        0x0400    /* length will be first received byte */
    __u16 len;        /* msg length                */
    __u8 *buf;        /* pointer to msg data            */
};

         第一次看到觉得怪怪的,虽然我知道宏定义作用于开始定义处,结束于#undef。网上也看到一些人的疑问,比如:

             a) 如果去定义这样的结构体变量,会不会导致一个宏重复定义。 

             b) 既然这样没错误,那么有什么好处。


2.    通过看如下程序预编译前后的结果:

预编译前:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct mystr {
        #define XPOS 1
        #define YPOS 2
        int a;
        char b;
};
      
int main(void)
{
        struct mystr ex1 = {2, 'a'};
        struct mystr ex2 = {3, 'b'};
        ex1.a=XPOS;
        ex2.a=YPOS;
              
        return 0;
}


执行命令:gcc main.c -E > main.E

预编译后:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 1 "main.c"
# 1 ""
# 1 ""
# 1 "main.c"
      
      
struct mystr {
      
      
 int a;
 char b;
};
      
int main(void)
{
 struct mystr ex1 = {2, 'a'};
 struct mystr ex2 = {3, 'b'};
 ex1.a=1;
 ex2.a=2;
              
              
             
 return 0;
}


结论:

预编译时,宏被展开,宏定义处被拿掉了,所以接下来再用struct  mystr来定义变量时,里面的宏已经不存在了,所以并不会导致一个宏重复被定义。至于这样编写代码的好处,有人说是增加代码的可阅读性,让人知道这些宏只会用于这个结构体中,还有就是便于对这个结构体进行扩展。


3,对于宏编译结构体的扩张的理解如下:

#define CV_SEQUENCE_FIELDS() \
do{

int flags; \
int header_size; \
struct CvSeq* h_prev; \
struct CvSeq* h_next; \
struct CvSeq* v_prev; \
struct CvSeq* v_next; \
int total; \
int elem_size; \
char* block_max; \
152 CHAPTER 1. CXCORE
char* ptr; \
int delta_elems; \
CvMemStorage* storage; \
CvSeqBlock* free_blocks; \
CvSeqBlock* first;

}while(0)


typedef struct CvSeq
{
     CV_SEQUENCE_FIELDS();
} CvSeq;



这是opencv里面的CV_Seq结构体的定义方式,使用宏CV_SEQUENCE_FIELDS()进行定义,这种定义方式有助于用户进行自行扩展。如果想在结构体里面再加入些东西,可以直接使用:
typedef struct Myseq
{
 

      CV_SEQUENCE_FIELDS();
      type1 variable1;
      type2 variable2;
} Myseq;
这种形式进行定义。
阅读(3403) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~