今天,在Windows上写C语言程序,结果编写的结构体总是无法使用__attribute__((packed)),导致读取的二进制文件字节对不上。
检查CMakeLists.txt
和代码均没有发现明显错误。
···
typedef struct
{
data_packet_header header;
uint16_t* data; // 数据
} attribute((packed)) data_packet_s;
···
想到的第一个解决方法。在代码头加上
extern{
C
}
发现没有用。
搜了半天,得到的解决方法主要是在结构体前加上pragma pack(1)
;
#pragma pack(1)
typedef struct
{
data_packet_header header;
uint16_t* data; // 数据
} __attribute__((packed)) data_packet_s;
然后竟然就好了。
查询网络文章#pragma pack(1) 的意义是什么。
解释为 #pragma pack(1)让编译器将结构体数据强制连续排列。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)