0

例如

typedef union
{
    struct
    { 
        unsigned fb_num                :  8 __attribute__ ((packed));
        unsigned stream_id             : 12 __attribute__ ((packed));
        unsigned lossless              :  1 __attribute__ ((packed));
        unsigned proto_idx             :  6 __attribute__ ((packed));
        unsigned da_logport            : 13 __attribute__ ((packed));
        unsigned sa_spare_dx           : 10 __attribute__ ((packed));
        unsigned da_spare_idx          : 10 __attribute__ ((packed));
        unsigned mcast_index           : 12 __attribute__ ((packed));/* ISH_B*/
        unsigned mesh_idx              :  8 __attribute__ ((packed));
        unsigned in_mirror             :  4 __attribute__ ((packed));
        unsigned vlan_idx              : 12 __attribute__ ((packed));
        unsigned dis_stp               :  1 __attribute__ ((packed));
        unsigned trkbal_idx            :  5 __attribute__ ((packed));
...

我想排除此代码部分:__attribute__ ((packed)),以便编译器将其读取为:

typedef union
{
    struct
    { 
        unsigned fb_num                :  8 ;
        unsigned stream_id             : 12 ;
        unsigned lossless              :  1 ;
        unsigned proto_idx             :  6 ;
        unsigned da_logport            : 13 ;
        unsigned sa_spare_dx           : 10 ;
        unsigned da_spare_idx          : 10 ;
        unsigned mcast_index           : 12 ;/* ISH_B*/
        unsigned mesh_idx              :  8 ;
        unsigned in_mirror             :  4 ;
        unsigned vlan_idx              : 12 ;
        unsigned dis_stp               :  1 ;
        unsigned trkbal_idx            :  5 ;
...

现在应该编译的宏是MCAST_SIMULATION:即如果MCAST_SIMULATION已定义,则排除提到的代码部分,否则不排除。我怎样才能做到这一点?

4

3 回答 3

1

__attribute__ ((packed))用宏 替换文本(例如PACKED,并有条件地定义:

#ifndef MCAST_SIMULATION
#define PACKED __attribute__ ((packed))
#else
#define PACKED
#endif
于 2013-09-10T11:21:41.667 回答
1

你定义__attribute__()为无。

像这样:

#ifdef MCAST_SIMULATION
# define __attribute__(x)
#endif
于 2013-09-10T11:14:07.447 回答
1

您可以为此引入自己的宏。就像是:

#ifndef MCAST_SIMULATION
#define ATTRIB_PACKED __attribute__((packed))
#else
#define ATTRIB_PACKED
#endif

用法:

unsigned fb_num :  8 ATTRIB_PACKED ;
于 2013-09-10T11:19:10.697 回答