Jul*_*ien 0 c struct pragma compiler-warnings
我有一个C主文件,其中包括此.h文件:
#pragma pack(1)
#ifndef PACKAGE
#define PACKAGE
struct A {
uint8_t a;
uint8_t b;
uint64_t c;
} typedef A;
#endif
Run Code Online (Sandbox Code Playgroud)
编译警告后:
myfile.c:28:10: warning: the current #pragma pack alignment value is modified in
the included file [-Wpragma-pack]
#include "structures.h"
^
./structures.h:1:9: note: previous '#pragma pack' directive that modifies
alignment is here
#pragma pack(1)
Run Code Online (Sandbox Code Playgroud)
出现。
我不明白我的代码有什么问题。有什么办法可以删除此警告?
这是一个完整的示例:
这是一个名为“ myfile.c”的简单C文件:
#include "structures.h"
int main(){
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是一个名为“ structures.h”的.h文件:
#include <stdlib.h>
#include <stdio.h>
#pragma pack(1)
#ifndef PACKAGE
#define PACKAGE
struct A {
uint8_t a;
uint8_t b;
uint64_t c;
} typedef A;
#endif
Run Code Online (Sandbox Code Playgroud)
警告是:
myfile.c:2:10: warning: the current #pragma pack alignment value is modified in
the included file [-Wpragma-pack]
#include "structures.h"
^
./structures.h:5:11: note: previous '#pragma pack' directive that modifies
alignment is here
#pragma pack(1)
^
1 warning generated.
Run Code Online (Sandbox Code Playgroud)
也许您需要阅读有关Pragma的GCC手册—§6.61.10Structure-Layout Pragmas。您可以明智地使用:
#ifndef PACKAGE
#define PACKAGE
#pragma pack(push, 1)
typedef struct A {
uint8_t a;
uint8_t b;
uint64_t c;
} A;
#pragma pack(pop)
#endif /* PACKAGE */
Run Code Online (Sandbox Code Playgroud)
我不知道这是否适用于所有与您相关的编译器。
顺便说一句,我将typedef关键字移到开头。C语法将其typedef视为存储类,并且(C11§6.11.5存储类说明符)还规定,存储类说明符在声明中不在声明说明符开头的位置是陈旧的功能。typedef首先输入关键字!
我还注意到,此标头不是自包含的(尽管由于标头保护,它是幂等的)。它依赖于<stdint.h>(或也许<inttypes.h>已经)包含在内。理想情况下,您应该#include <stdint.h>在第#pragma一个标头之前添加,以便即使编译单元中包含的第一个标头也可以编译代码。另请参阅我应该使用#include内部标题吗?
| 归档时间: |
|
| 查看次数: |
139 次 |
| 最近记录: |