我正在用 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 --version
gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4
最佳答案
与 GCC manual 相比,您的 push
和 pack
语法是倒退的表示支持。 #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)
虽然链接是GCC 5.2.0的文档,但是把URL中的5.2.0改成4.8.4也是一样的。
顺便说一下,对于显示的Packet
数据结构,打包结构的方向在任何合理的机器架构上都是多余的。有很多结构不是这种情况(事实上,PacketNode
结构就是这样一种结构,尽管我认为打包它没有任何好处,因为它不是通过网络发送的),但是 Packet
结构在打包和解包形式中的布局相同。
关于c - #Pragma pack 编译时发出警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33611737/