包含的 C 文件中的 Pragma
Pragma in included C file
我有一个 C 主文件,其中包含此 .h 文件:
#pragma pack(1)
#ifndef PACKAGE
#define PACKAGE
struct A {
uint8_t a;
uint8_t b;
uint64_t c;
} typedef A;
#endif
编译后警告:
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)
出现。
我不明白我的代码有什么问题。有什么办法可以删除这个警告吗?
这是一个完整的例子:
这是一个名为 "myfile.c" 的简单 C 文件:
#include "structures.h"
int main(){
return 0;
}
这是名为 "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
警告是:
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.
也许您需要阅读有关 pragma 的 GCC 手册 — §6.61.10 Structure-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 */
我不知道这是否适用于与您相关的所有编译器。
顺便说一句,我把typedef
关键字移到了开头。 C 语法将 typedef
视为 storage class, and also stipulates (C11 §6.11.5 Storage class specifiers) 即 放置 storage-class 说明符而不是在声明中的声明说明符的开头是过时的功能。先放关键词typedef
!
我还注意到这个 header 不是 self-contained(虽然它是幂等的,因为 header 守卫)。它依赖于已经包含的 <stdint.h>
(或者可能是 <inttypes.h>
)。理想情况下,您应该在第一个 #pragma
之前添加 #include <stdint.h>
,这样即使这是翻译单元中包含的第一个 header,代码也能编译。另见 Should I use #include
inside headers?
我有一个 C 主文件,其中包含此 .h 文件:
#pragma pack(1)
#ifndef PACKAGE
#define PACKAGE
struct A {
uint8_t a;
uint8_t b;
uint64_t c;
} typedef A;
#endif
编译后警告:
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)
出现。
我不明白我的代码有什么问题。有什么办法可以删除这个警告吗?
这是一个完整的例子:
这是一个名为 "myfile.c" 的简单 C 文件:
#include "structures.h"
int main(){
return 0;
}
这是名为 "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
警告是:
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.
也许您需要阅读有关 pragma 的 GCC 手册 — §6.61.10 Structure-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 */
我不知道这是否适用于与您相关的所有编译器。
顺便说一句,我把typedef
关键字移到了开头。 C 语法将 typedef
视为 storage class, and also stipulates (C11 §6.11.5 Storage class specifiers) 即 放置 storage-class 说明符而不是在声明中的声明说明符的开头是过时的功能。先放关键词typedef
!
我还注意到这个 header 不是 self-contained(虽然它是幂等的,因为 header 守卫)。它依赖于已经包含的 <stdint.h>
(或者可能是 <inttypes.h>
)。理想情况下,您应该在第一个 #pragma
之前添加 #include <stdint.h>
,这样即使这是翻译单元中包含的第一个 header,代码也能编译。另见 Should I use #include
inside headers?