如何在Linux中將下面的vC++打包命令轉換爲gcc命令?我知道如何做一個單一的結構,但我該如何做一系列的結構?如何將struct packing從vC++轉換爲gcc
#pragma pack(push, 1) // exact fit - no padding
//structures here
#pragma pack(pop) //back to whatever the previous packing mode was
如何在Linux中將下面的vC++打包命令轉換爲gcc命令?我知道如何做一個單一的結構,但我該如何做一系列的結構?如何將struct packing從vC++轉換爲gcc
#pragma pack(push, 1) // exact fit - no padding
//structures here
#pragma pack(pop) //back to whatever the previous packing mode was
您可以添加屬性((包裝))向個別數據項來實現這一目標。在這種情況下,打包應用於數據項目,因此不需要恢復舊模式。
例: 對於結構:
typedef struct _MY_STRUCT
{
}__attribute__((packed)) MY_STRUCT;
對於數據成員:
struct MyStruct {
char c;
int myInt1 __attribute__ ((packed));
char b;
int myInt2 __attribute__ ((packed));
};
gcc還支持那些編譯指示。請參閱編譯器文檔: http://gcc.gnu.org/onlinedocs/gcc/Structure_002dPacking-Pragmas.html
另外,您可以使用更具體的GCC-
__attribute__(packed)
例如:
struct foo {
int16_t one;
int32_t two;
} __attribute__(packed);
http://gcc.gnu.org/onlinedocs/gcc-3.3.6/gcc/Type-Attributes.html
是的。我實際上知道如何爲單個結構做到這一點。我如何做到這一點的結構範圍? –
由於gcc支持編譯指示包編譯器命令,爲什麼不以同樣的方式執行? – segfaultomatic
thx segfaultomatic –
據http://gcc.gnu.org/onlinedocs/gcc/Structure_002dPacking-Pragmas.html,GCC應該支持#pragma pack
直接,所以你可以直接使用它。
指定比對的gcc way
是__attribute__((aligned(x)))
,其中x
需要對齊。
您也可以使用__attribute__((packed))
來指定緊湊的結構。
參考http://gcc.gnu.org/onlinedocs/gcc-3.2/gcc/Type-Attributes.html
由於GCC支持編譯指示包的編譯器的命令,我們可以與上面的代碼粘。 –
看到這個... [鏈接](http://stackoverflow.com/questions/1537964/visual-c-equivalent-of-gccs-attribute-packed?rq=1) – NeonGlow
thx氖,我會測試我的程序再仔細一點 –