2016-07-06 40 views
0

我有一个C文件:编译C结构来bin文件

#include <stdint.h> 

typedef struct { 
    uint32_t m1; 
    uint16_t m2; 
} TStruct; 

TStruct s = { 
    1, 
    2 
}; 

如何编译用gcc产生小尾数二进制文件这个文件?

0x01 0x00 0x00 0x00 0x02 0x00 
+0

编译它为一个使用little endian的arch ...而且你还必须照顾对齐。 – LPs

回答

-1

大概

objcopy -O binary 

是你在找for.See [ this ]什么事这么回答。

0

虽然Little-Endian是所有标准配置的默认设置。其他配置的编译器工具包可以使用此开关来获取所需的格式。

gcc file.c -mlittle-endian 

你去那里..

0

我猜搭售其他答案在一起。在一个小端机器(x86)上:

[email protected] ~/se $ cat foo.c 
#include <stdint.h> 

typedef struct { 
    uint32_t m1; 
    uint16_t m2; 
} TStruct; 

TStruct s = { 
    1, 
    2 
}; 
[email protected] ~/se $ gcc -c foo.c 
[email protected] ~/se $ objcopy -O binary foo.o foo 
[email protected] ~/se $ hexdump -C foo 
00000000 01 00 00 00 02 00 00 00       |........| 
00000008