#pragma pack(show) with GCC
#pragma pack(show) with GCC
有没有办法用GCC显示内存"包"大小?
在Microsoft Visual C++中,我使用的是:
#pragma pack(show)
其在警告消息中显示该值;请参阅Microsoft文档。
GCC的等价物是什么?
由于我在相关文档中看不到这样的功能,我将得出结论,GCC无法做到这一点。
每当我打包一个结构并想查看其大小时,我都会使用静态断言。
/*
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);
每当静态断言失败时,这将向您发出编译时警告。
相关文章:
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 奇怪的结构&GCC&clang(void*返回类型)
- GCC本机矩阵运算库
- PowerPC ppc64le上的Gcc Woverloaded虚拟错误
- gcc和c++17的过载解析失败
- 数据成员SFINAE的C++17测试:gcc vs clang
- GCC对可能有效的代码抛出init list生存期警告
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 如何在 GCC 中启用 C/C++ "Conditional with Omitted Operand"(又名猫王操作员 "?:" )
- 在独立(裸金属)项目上使用std::tuple with arm none eabi gcc
- 不理解 gcc --with-local-prefix, --with-native-system-header-dir=
- Boost 1.49/1.50/1.51 ASIO with MinGW & GCC 4.7.0 中的转换错误
- gcc 未给出的 Clang 错误"attempted to construct a reference element in a tuple with an rvalue"
- GCC with Cygwin/Win7: missing syscall.h
- GCC with Windows SDK?
- compiling .cpp with gcc
- icpc C++11 with gcc stdlib
- templates C++ with GCC
- Eclipse CDT with MinGW GCC Make Error 127
- #pragma pack(show) with GCC