簡體   English   中英

clang關於__attribute __((packed)),即使需要打包結構

[英]clang compaining about __attribute__((packed)) even though the struct needs to be packed

我有一個結構,需要打包(沒有打包大小是20字節,但我需要16才能讀/寫)。 當我添加了packed屬性時,我得到了error: packed attribute is unnecessary for struct的所有成員的警告, error: packed attribute is unnecessary for的。 當用pragma沉默錯誤時,代碼編譯得很好,結構的大小是16,但是如果我刪除了pragma就失敗了(因為我正在使用-Werror )。 clang是不是錯誤地發出了這個警告,還是我做錯了什么?

#include <cstdint>

typedef struct __attribute__((packed))
{
    uint16_t wFormatTag;
    uint16_t nChannels;
    uint32_t nSamplesPerSec;
    uint32_t nAvgBytesPerSec;
    uint16_t nBlockAlign;
}
WAVEFORMAT;

int main()
{
    WAVEFORMAT w;
    (void)w;
}

我正在使用Xcode9工具鏈:

clang -v
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin16.7.0

使用-Weverything編譯時會生成警告:

clang++ -Weverything pack.cpp

pack.cpp:8:10: warning: packed attribute is unnecessary for 'wFormatTag' [-Wpacked]
    WORD wFormatTag;
        ^
...

事實上,clang似乎錯誤地發出了警告。 這已在最近的clang版本中修復。

特別是,警告不會與clang 6.0候選clang version 6.0.0-svn323772-1~exp1clang version 6.0.0-svn323772-1~exp1 )一起clang version 6.0.0-svn323772-1~exp1 我能夠使用以前的clang版本復制警告,包括5.0.1,4.0.0和3.8.0。

我相信這是修正問題的鏗鏘提交:
https://reviews.llvm.org/D34114

不幸的是,它可能需要一些時間,直到Xcode的鏗鏘看到這個修復。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM