VISUAL C ++ GCC的__attribute__的当量((__packed__)) [英] Visual C++ equivalent of GCC's __attribute__ ((__packed__))

查看:1103
本文介绍了VISUAL C ++ GCC的__attribute__的当量((__packed__))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关一些编译器,有一个结构包装说明,例如:

For some compilers, there is a packing specifier for structs, for example ::


RealView ARM compiler has "__packed"
Gnu C Compiler has "__attribute__ ((__packed__))"
Visual C++ has no equivalent, it only has the "#pragma pack(1)"

我需要的东西,我可以投入的结构的定义。

任何信息/破解/建议吗? TIA ...

Any info/hack/suggestion ? TIA...

推荐答案

我不知道这样做的一个圆滑的方式,但你可能做这样的事情太可怕了:

I don't know a slick way of doing it, but you could possibly do something horrible like this:

#include "packed.h"
struct Foo { /* members go here */ } PACKED;
#include "endpacked.h"

那么对于MSVC,packed.h:

Then for MSVC, packed.h:

#define PACKED
#pragma pack(push,1)

endpacked.h

endpacked.h

#pragma pack(pop)
#undef PACKED

有关GCC,packed.h:

For gcc, packed.h:

#define PACKED __attribute__ ((__packed__))

endpacked.h:

endpacked.h:

#undef PACKED

从根本上讲,包装是太依赖于平台的。假设你的包装结构中有8位字段,并考虑一些系统,具有16位字节。它不能有一个结构重新$ P $只是包装psenting您的数据 - 你必须知道如何在两个系统之间传输8位字节转换为16位字节。 16bit的机器上的结构可能需要位域,在这种情况下,你必须知道如何实施奠定了出来。

Fundamentally, packing is too platform-dependent. Suppose your packed struct has 8-bit fields in it, and consider some system with a 16-bit byte. It can't have a struct representing your data just by packing - you'd have to know how 8-bit bytes are converted to 16-bit bytes when transferred between the two systems. The struct on the 16bit machine might need bitfields, in which case you'd have to know how the implementation lays them out.

因此​​,如果code旨在成为移植,你可能只需要确定你在你的头文件中的特定于平台的部分需要的任何包装的结构。或者说,你的结构code,使未来的端口可以这样做,如果它有。

So if the code is intended to be generally portable, you may just have to define whatever packed structures you need in a platform-specific section of your header file. Or rather, structure your code so that a future port can do that if it has to.

这篇关于VISUAL C ++ GCC的__attribute__的当量((__packed__))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆