#pragma pack(show)与GCC

时间:2014-04-29 14:36:36

标签: c++ c visual-c++ gcc

有没有办法用GCC显示内存“打包”大小?

在Microsoft Visual C ++中,我正在使用:

 #pragma pack(show) 

在警告消息中显示值;见Microsoft's documentation

与GCC相同的是什么?

2 个答案:

答案 0 :(得分:2)

由于我无法在the pertinent documentation中看到此类功能,因此我将得出结论,GCC无法做到这一点。

答案 1 :(得分:1)

每当我打包一个结构并希望看到它的大小时,我就会使用静态断言。

/*
   The static_assert macro will generate an error at compile-time, if the predicate is false
   but will only work for predicates that are resolvable at compile-time!

   E.g.: to assert the size of a data structure, static_assert(sizeof(struct_t) == 10)
*/
#define STATIC_ASSERT(COND,MSG)      typedef char static_assertion_##MSG[(!!(COND))*2-1]
/* token pasting madness: */
#define COMPILE_TIME_ASSERT3(X,L)     STATIC_ASSERT(X,at_line_##L)             /* add line-number to error message for better warnings, especially GCC will tell the name of the variable as well */
#define COMPILE_TIME_ASSERT2(X,L)     COMPILE_TIME_ASSERT3(X, L)               /* expand line-number */
#define static_assert(X)              COMPILE_TIME_ASSERT2(X, __LINE__)        /* call with line-number macro */

#define PACKED  __attribute__ ((gcc_struct, __packed__))

typedef struct {
  uint8_t bytes[3];
  uint32_t looong;
} PACKED struct_t;
static_assert(sizeof(struct_t) == 7);

每当静态断言失败时,这将为您提供编译时警告。