2012-12-18 47 views
1

如何在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 
+0

由於GCC支持編譯指示包的編譯器的命令,我們可以與上面的代碼粘。 –

+1

看到這個... [鏈接](http://stackoverflow.com/questions/1537964/visual-c-equivalent-of-gccs-attribute-packed?rq=1) – NeonGlow

+0

thx氖,我會測試我的程序再仔細一點 –

回答

3

您可以添加屬性((包裝))向個別數據項來實現這一目標。在這種情況下,打包應用於數據項目,因此不需要恢復舊模式。

例: 對於結構:

typedef struct _MY_STRUCT 
{ 

}__attribute__((packed)) MY_STRUCT; 

對於數據成員:

struct MyStruct { 

    char c; 

    int myInt1 __attribute__ ((packed)); 

    char b; 

    int myInt2 __attribute__ ((packed)); 

}; 
1

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

+0

是的。我實際上知道如何爲單個結構做到這一點。我如何做到這一點的結構範圍? –

+1

由於gcc支持編譯指示包編譯器命令,爲什麼不以同樣的方式執行? – segfaultomatic

+0

thx segfaultomatic –

相關問題