2010年4月23日 星期五

gcc cross compiler union compatible issue.



有些platform struct or union 預設為4bytes(一般會預期應該要2bytes)
目前可用
__attribute__((__packed__))來解決
其它的code要小心有沒有類似的問題


typedef union _MACHTTRANSMIT_SETTING
{
        unsigned short word;
        union //short 2 bytes,但有些可能 assign 4 bytes
        {
                unsigned short MCS:7; // MCS
                unsigned short BW:1; //channel bandwidth 20MHz or 40 MHz
                unsigned short ShortGI:1;
                unsigned short STBC:2; //SPACE
                unsigned short rsv:3;
                unsigned short MODE:2; // Use definition MODE_xxx.
        } __attribute__((__packed__))field;
}__attribute__((__packed__)) MACHTTRANSMIT_SETTING, *PMACHTTRANSMIT_SETTING;

int main(int argc, char *argv[])
{
        MACHTTRANSMIT_SETTING m;
        printf("m: %d, m->word:%d@%p\n", sizeof(m), sizeof(m.word), &m.word);
        printf("m: %d, m->:%d@%p\n", sizeof(m), sizeof(m.word), &m.word);
        printf("ok: %d, m->:%d@%p\n", sizeof(m), sizeof(m.field), &m.field);
}
X86
others
peter@team-server:~$ ./a.out
m: 2, m->word:2@0xbff97a32
m: 2, m->:2@0xbff97a32
ok: 2, m->:1@0xbff97a32
/tmp/rootfs # ./a.out
m: 2, m->word:2@0x4261fcd4
m: 2, m->:2@0x4261fcd4
ok: 2, m->:1@0x4261fcd4


沒有留言: