如何为可移植程序正确使用 gcc -fsso-struct?
How to properly use gcc -fsso-struct for portable programs?
我正在尝试使用 gcc 选项 -fsso-struct 来设置可移植的位域布局(我认为这是 gcc-6 的特性 https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html)。
目前,我有两套位域结构:一套用于大端,一套用于小端。我使用 BYTE_ORDER 标志来选择一个或另一个 (https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html)。
我将 -fsso-struct 设置为 big endian 和 little endian,但我没有看到解释位域的区别。我应该如何使用它?
查看文档中的警告:
Warning: the -fsso-struct
switch causes GCC to generate code that is not binary compatible with code generated without it if the specified endianness is not the native endianness of the target.
任何带有此类警告的选项都是您在与任何库(甚至是标准库)交互时不应使用的选项。
此选项也以本地形式提供,可以通过 #pragma
指令安全地使用。这些函数:
struct A { int i; };
#pragma scalar_storage_order big-endian
struct B { int i; };
#pragma scalar_storage_order little-endian
struct C { int i; };
#pragma scalar_storage_order default
struct D { int i; };
int fa(struct A a) { return a.i; }
int fb(struct B b) { return b.i; }
int fc(struct C c) { return c.i; }
int fd(struct D d) { return d.i; }
导致 struct A
和 struct D
具有平台的默认字节序,struct B
为大端,struct C
为小端。因此,fa
、fd
以及 fb
或 fc
都可以编译为直接从参数加载值的代码。其余函数 fc
或 fb
将包含交换字节的代码。
如您所见,此选项与 bit-fields 无关。它确实也会影响 bit-fields,但这只是因为 bit-fields 存储整数值,而此选项会影响整数存储。
我正在尝试使用 gcc 选项 -fsso-struct 来设置可移植的位域布局(我认为这是 gcc-6 的特性 https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html)。
目前,我有两套位域结构:一套用于大端,一套用于小端。我使用 BYTE_ORDER 标志来选择一个或另一个 (https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html)。
我将 -fsso-struct 设置为 big endian 和 little endian,但我没有看到解释位域的区别。我应该如何使用它?
查看文档中的警告:
Warning: the
-fsso-struct
switch causes GCC to generate code that is not binary compatible with code generated without it if the specified endianness is not the native endianness of the target.
任何带有此类警告的选项都是您在与任何库(甚至是标准库)交互时不应使用的选项。
此选项也以本地形式提供,可以通过 #pragma
指令安全地使用。这些函数:
struct A { int i; };
#pragma scalar_storage_order big-endian
struct B { int i; };
#pragma scalar_storage_order little-endian
struct C { int i; };
#pragma scalar_storage_order default
struct D { int i; };
int fa(struct A a) { return a.i; }
int fb(struct B b) { return b.i; }
int fc(struct C c) { return c.i; }
int fd(struct D d) { return d.i; }
导致 struct A
和 struct D
具有平台的默认字节序,struct B
为大端,struct C
为小端。因此,fa
、fd
以及 fb
或 fc
都可以编译为直接从参数加载值的代码。其余函数 fc
或 fb
将包含交换字节的代码。
如您所见,此选项与 bit-fields 无关。它确实也会影响 bit-fields,但这只是因为 bit-fields 存储整数值,而此选项会影响整数存储。