#Pragma pack 编译时发出警告
#Pragma pack giving warning when compiling
我正在用 C 语言创建以下结构,只有数据包结构将通过 UDP 套接字发送。我想打包结构以避免在不同的计算机上进行不同的对齐(不确定我是否需要同时打包或只打包我发送的那个)。当我编译它时,我收到以下警告:
warning: ignoring #pragma ( [-Wunknown-pragmas] #pragma(pack);`
为什么我的编译指示被忽略了?以及如何解决这个问题。
#pragma push(pack, 1);
struct packet{ // the actual packet within the node
uint32_t seqnum;
uint32_t checkSum;
uint32_t numPackets; // number of packets to send
char data[1024]; // the data in the packet
};
struct packetNode{ //for the linked list
struct packet p;
struct packetNode *next;
};
#pragma pop(pack)
gcc 版本:
gcc --version
gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4
您的 push
和 pack
语法与 GCC manual 所支持的语法相比是倒退的。 #pragma
后的分号是多余的。
#include <inttypes.h>
#pragma pack(push, 1)
struct packet{ // the actual packet within the node
uint32_t seqnum;
uint32_t checkSum;
uint32_t numPackets; // number of packets to send
char data[1024]; // the data in the packet
};
struct packetNode{ //for the linked list
struct packet p;
struct packetNode *next;
};
#pragma pack(pop)
虽然link是GCC 5.2.0的文档,但是把URL里面的5.2.0改成4.8.4也是一样的。
顺便说一下,对于显示的 Packet
数据结构,打包结构的方向在任何合理的机器架构上都是多余的。有很多结构不是这种情况(事实上,PacketNode
结构就是这样一种结构,尽管我认为打包它没有任何好处,因为它不是通过网络发送的),但是 Packet
结构在打包和解包形式中的布局将相同。